Find Anagrams in a string. You may use a different algorithm as long as
it's as good as this one.
- Ask the user for a string.
If the string is too long, complain. Remember that 10! is three
million and even 7! is over five thousand.
- Look in the file /usr/share/dict/words for a list of words.
Load them into an associative array.
- Construct every permutation of the original word.
There is code below that might help.
- If any of those permutations appear in the assocative array,
print it with success.
- Partition each permutation into two smaller words. If both
of those words appear on the list, print them.
Bonus Points
- A Progress bar. See this javascript for an example.
- A Progress bar that approximates the actual progress.
- The little icon in the URL bar.
- Anything else.
Code ... stolen from
http://www.cut-the-knot.org/do_you_know/AllPerm.shtml
#include
using namespace std;
const int N = 7;
char Value[N+1];
void getNext()
{
int i = N - 1;
while (Value[i-1] >= Value[i])
i = i-1;
int j = N;
while (Value[j-1] <= Value[i-1])
j = j-1;
//swap values at positions (i-1) and (j-1)
char tmp = Value[i-1];
Value[i-1] = Value[j-1];
Value[j-1] = tmp;
i++;
j = N;
while (i < j)
{
// swap(i-1, j-1);
tmp = Value[i-1];
Value[i-1] = Value[j-1];
Value[j-1] = tmp;
i++;
j--;
}
}
int main()
{
Value[0]='a';
Value[1]='b';
Value[2]='c';
Value[3]='c';
Value[4]='c';
Value[5]='c';
Value[6]='c';
Value[7]=0;
while(1) {
cout << Value << endl;
getNext();
}
}