上下文:我正在用C ++编写一个赋值,其中用户输入一个单词或一个句子以逐个单词为基础进行解读。我有一个充满英文单词的文本文件,我已经读入unordered_set字符串中。然后,我检查每个输入单词的排列,并尝试在unordered_set中找到它。未被打扰的单词可能性被打印出来给用户。
问题:文本文件中有很多单词。该程序无法正常运行,因为遍历所有排列并在unordered_set中查找匹配项花费的时间太长。
可能的解决方案:我想限制搜索单词的范围,因为文本文件已经按字母顺序排列。例如,如果加扰的单词是“ cit”,则该单词的一个排列将是“ itc”。我想搜索以i开头的unordered_set中的所有单词,以查找“ itc”。
这是我到目前为止所拥有的。
void unscramble() {
//issue - too slow, find in range?
string word;
string temp;
ifstream inDictionaryFile("words_alpha.txt");
unordered_set<string> dictionary;
//read dictionary file into a unordered_set
while (getline(inDictionaryFile, temp)) {
auto result = dictionary.insert(temp + " ");
}
cout << "Enter something to unscramble: ";
//find/print out matches for permuations of scrambled words
while (cin>>word) {
do {
word = word + " ";
auto result = dictionary.find(word);
if (result != end(dictionary)) {
cout << setw(10) << word;
}
} while (next_permutation(begin(word), end(word)));
}
}
答案 0 :(得分:0)
如果仅需要前3个字母的排列,则可以使用unordered_multiset,其键等于规范排列(例如,排序的前3个字母)。但是我想,您所遇到的实际问题不应仅用一种数据结构来解决,而应该用数种结构来解决,一种用于存储的数据结构,另一种用于对该存储的索引的数据结构。