This Post is about a Code that I wrote for solving Anagrams.It all started with this bot friend in my friend list : games@gtalkbots.com.It is a bot that allows users to play anagram with other users.
I was(rather am) really pathetic at solving anagrams instantaneously.Instead of sulking, I thought I must do something about it. I could win each round if I could get an answer within 0.1 secs. (My own Heuristic).
So,The Best way is to write a C++ Code for the same.Then,after little brain storming (random thoughts actually), I just needed a Dictionary and a Data Structure to store it. Since Hashing was out of question for a Quick-Code,STL Set was the second Best with Insertion /Look up of Asymptotic Upper Bound of O(log(n)).So, I just had to check all Permutations of the Random String and look up the Dictionary and if Match is found then Output the Result.
Within first 2 results for Dictionary in Google, I found a US Dictionary List in txt format.
So, The C++ Code for the same ::
#include<iostream>
#include<string>
#include<cstdlib>
#include<set>
#include<fstream>
using namespace std;
// This is a Set that Stores the Dictionary Values.
set<string>s;
/*
* This Method is used to check whether the String matches the given Mask.
* Example : srinivas --i-i--s
*/
bool check(string a, string b) {
for (int i = 0; i < a.size(); i++)
if (isalpha(a[i]) && isalpha(b[i]) && a[i] != b[i])
return false;
return true;
}
/*
* This Method is used to find all Permutations of the String
*/
void perm(string pre, string suff) {
if (!suff.size())
cout << (s.find(pre) != s.end() ? pre + '\n' : "");
else
for (int i = 0; i < suff.size(); i++) {
perm(pre + suff[i], suff.substr(0, i) + suff.substr(i + 1, suff.size()));
}
}
int main() {
s.clear();
string a;
ifstream in;
in.open("US.dic");
while (in >> a)
s.insert(a);
in.close();
cout << s.size() << endl;
while (cin >> a) {
perm("", a);
cout << "----------------------\n";
}
return 0;
}
But then as Luck would have it, The Bot went offline after I wrote the Code. I guess both of them are somehow related. But then Next Time I find the bot online,you wouldn't wonder if you find my name as one of the Top-Scorer's.
Cheers!!!
Do Comment/Post about it.