1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iterator>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////
int uniqueIndex(vector<int> indices, string str, vector<string> vec, int a)
{
int it = find(vec.begin() + a,vec.end(),str) - vec.begin();
if(find(indices.begin(),indices.end(),it) == indices.end())
{
cout<<"value returned "<<it; //testing
return it;
}
uniqueIndex(indices,str,vec,a + it + 1);
// return 0;
/* if i include this return 0, then 0 is received by the calling function.
why is the reutrn it; above not executed for last iteration.
*/
}
//////////////////////////////////////////////////////////////////////////////////
bool anagram(string a, string b)
{
for(int i = 0; i < a.length(); i++)
{
if(!isalpha(a.at(i)))
{
a.erase(a.begin()+i);
i--;
}
}
for(int i = 0; i < b.length(); i++)
{
if(!isalpha(b.at(i)))
{
b.erase(b.begin()+i);
i--;
}
}
transform(a.begin(),a.end(),a.begin(),tolower);
transform(b.begin(),b.end(),b.begin(),tolower);
sort(a.begin(), a.end());
sort(b.begin(), b.end());
return (a == b);
}
void SameCharIDX(string in, vector<vector<int> > & out)
{
vector<string> vec;
//now tokenize in.
string xx; //each word
stringstream ss;
ss << in; //put in in ss
while (getline(ss, xx, ','))
vec.push_back(xx);
vector<string> temp(vec); //copy vec to temp.
for (int i = 0; i < vec.size(); i++)
cout << vec.at(i) << " , ";
cout << endl;
for (int i = 0; i < temp.size(); i = 0) //look critically
{
string key = temp.at(i);
vector<int> indices; //indeces of anagrams
for (int j = 0; j < temp.size(); j++)
{
if (anagram(key, temp.at(j))) //inluding temp(0), coz a word is an anagram of iteself
{
////////////////////////////////////////////////////////////////
int index = uniqueIndex(indices,temp.at(j),vec,0);
////// //////
cout<<" - "<<index<<" Value revieved"<<endl;
/////////////////////////////////////////////////////////////////
indices.push_back(index);
temp.erase(j + temp.begin());
--j;
}
}
out.push_back(indices);
}
return ;
}
int main()
{
string in = "Book, c++ is great, Boko, cigarets, time, koob, Mite, time";
vector<vector<int>> out;
SameCharIDX(in, out);
cout << endl<<endl<<"Now printing the index of words with identical alphabetic chars\n";
for (int i = 0; i < out.size(); i++) //print vector
{
if(out[i].size() > 1) //print only if there is more than one anagram.
{
for (int j = 0; j<out[i].size(); j++) //5
{
cout << out[i][j] << ", ";
}
cout << endl;
}
}
system("pause");
return 0;
}
|