word/text twist program, assistance required

Pages: 12
Mitsakos: I may have misunderstood something, but why not just use a set<string>? And what did you put in the value?
The OP posted no such restriction. But it would be simple enough to first check that the word length is the same as the number of letters to check, check for invalid letters, then check that each letter appears only once. That would be slower, but it would be the optimal solution.

One way to implement that would indeed be to use count().

:-)

[edit] He could use a set, but it would be a lot more overhead to do the same thing.
Last edited on
@helios
I have no idea what a set<string> is...
I used a map<string, string> and read the words file in the mpa like that:

1
2
3
4
5
6
7
8
9
10
map<string, string> words;
string dictWord;
ifstream inFile( inDict.c_str() );
if( inFile.is_open() ){
   while( getline( inFile, dictWord ) )
      words[dictWord] = dictWord;
   inFile.close();
}else{
   cout << "Error opening file...\n";
}

After that when I make every possible compination I use:
1
2
if( !words[str].empty() )
   cout << str << " is a possible word\n";


I think that this was a good reason to use maps. They make the searching through their contents using a keyword much faster than just searching a vector, and since the keyword is the same as the word, if it exists I have a word.

[EDIT]
I started reading about sets now and you are probably right. I didn't know about it. Thanx.
Last edited on
I brought up permutations because the words he is looking for are up to 7 letters long, so there will be no problem generating these very quickly... if the words were up to 28 letters long I wouldn't have suggested it.
I wasn't attacking you. The reason I suggest against mixing letters and looking for valid words was demonstrated quite nicely by my extreme example. (There is only one word that is 28 letters long. :-)

For 7-letter words it would be 823,543 permutations, which is more than four times as many words in the OWL.

By only looking for known words, we avoid the permutations altogether and obviate the lookup step to see if we have a valid word. That is a single scan over the word list, and a single scan over each word for invalid letters. That would take significantly less time.

That's all I meant.
Last edited on
This is how I did my code. Work just fine. hehe. I just eliminate all the mismatch one by one without permutating.

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
#include <iostream> 
#include <vector>
const int MAXSIZE = 10; 
using namespace std; 

int main(){
	int count(0);
	vector<string> myvector;
	cout << "Please input word: "; 
		string input;
		cin >> input; 
	string wordlist[MAXSIZE];
		wordlist[0] = "ABCD";
		wordlist[1] = "Hello";
		wordlist[2] = "DCBA"; 
		wordlist[3] = "olleH";
		wordlist[4] = "ollHe";
		wordlist[5] = "CDBA"; 
		wordlist[6] = "bro";
		wordlist[7] = "orb";
		wordlist[8] = "rob";
		wordlist[9] = "bor"; 

		for(int i = 0; i <= MAXSIZE; i++){ //loops through list. 
			if(input.length() == wordlist[i].length()){
				for(int k = 0; k <= input.length(); k++){ //loops through letter of word with same length. 
					for(int j = 0; j < wordlist[i].length(); j++){ 
						if(input[k] == wordlist[i][j]) //check how many char matches.  
						count++; 
					}
				}
				if(input.length() == count){ //if all char matches. 
					myvector.push_back(wordlist[i]);
				}		
			}
			count = 0; 
		}
			cout << "The following word from list match: " << endl; 
			for(int x = 0; x < myvector.size(); x++){
				cout << myvector[x] << endl;
			}
}//main 
Using Duoas solution with the find_first_not_of() and the count() functions I came up with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//words is a vector that holds all the words from a dictionary file
//letters is a string that holds the letters the user enters
//the bool "asymfonia" is to compare how many same letters the word from
//the dictionary and the letters of the user have (for example if the letters
//from the user are "bot" then the "boot" will be illegal, but "bot" from letters boot is legal).
for( vector<string>::iterator it = words.begin(); it != words.end(); ++it ){
   size_t found = it->find_first_not_of( letters );
   if( found == string::npos ){
      bool asymfonia = false;
      for( string::iterator sit = it->begin(); sit != it->end(); ++sit )
         if( count( letters.begin(), letters.end(), *sit ) < count( it->begin(), it->end(), *sit ) )
            asymfonia = true;
      if( !asymfonia )
         cout << "Found word: " << *it << '\n';
         //keyWords( *it ); //I originally send my word to a function
   }
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12