Created
February 25, 2014 23:58
-
-
Save rptynan/ccfd94ab96427c27b175 to your computer and use it in GitHub Desktop.
AOI Anagram solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <vector> | |
#include <map> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
ifstream wordsin ("words.txt"); | |
ifstream anagin ("anagin.txt"); | |
ofstream anagout ("anagout.txt"); | |
string cur, words[651]; | |
int wordslen[650][26], curlen; | |
int W=0; | |
map<string,vector<int> > m; | |
int main(){ | |
while(1){ | |
wordsin>>words[W]; | |
if(words[W][0]=='#') break; | |
cur=words[W]; | |
sort(cur.begin(),cur.end()); | |
m[cur].push_back(W); | |
//cout<<'-'<<cur<<endl; | |
++W; | |
} | |
while(1){ | |
getline(anagin,cur); | |
if(cur[0]=='#') break; | |
sort(cur.begin(),cur.end()); | |
int i; | |
for(i=0; cur[i]==' '; ++i); | |
cur.erase(cur.begin(),cur.begin()+i); | |
//cout<<'>'<<cur<<endl; | |
if(m[cur].size()==0){ | |
anagout<<"No anagram found"<<endl; | |
} | |
else{ | |
vector<int>::iterator it; | |
for(it=m[cur].begin(); it!=m[cur].end(); ++it){ | |
anagout<<words[*it]<<' '; | |
} | |
anagout<<endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment