本文整理汇总了C++中Trie::AddWord方法的典型用法代码示例。如果您正苦于以下问题:C++ Trie::AddWord方法的具体用法?C++ Trie::AddWord怎么用?C++ Trie::AddWord使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Trie
的用法示例。
在下文中一共展示了Trie::AddWord方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
cout << "!!!Hello Trie !!!" << endl;
Trie* ac = new Trie;
ac->AddWord("California");
ac->AddWord("Colarado");
ac->AddWord("Alaska");
ac->AddWord("Arizona");
if ( ac->SearchWord("Barcelona") )
cout << "Found Barcelona" << endl;
else
cout << "Barcelona not found" << endl;
if ( ac->SearchWord("California") )
cout << "California found" << endl;
else
cout << "California not found" << endl;
}
示例2: main
int main(int argc, char** argv) {
if ( argc <= 1 ) {
cout << "Please enter the filename with sorted" <<
"words (lower case 'a' to 'z' without space)" << endl;
return 1;
}
Trie t;
queue<pair<string, string> > q;
char *file_name = argv[1];
ifstream ifs(file_name, ifstream::in);
string line;
while ( ifs >> line ) {
t.AddWord(line);
vector<pair<string, string> > word_prefix_pairs = t.GetAllPrefixWords(line);
if ( word_prefix_pairs.size() != 0 ) {
for ( int i = 0; i < word_prefix_pairs.size(); ++i ) {
q.push(word_prefix_pairs[i]);
}
}
}
int max_val = 0;
set<string> v;
while ( !q.empty() ) {
if ( t.ContainsWord(q.front().second)) {
v.insert(q.front().first);
max_val = max(max_val, (int)(q.front().first.size()));
q.pop();
} else {
vector<pair<string, string> > word_prefix_pairs = t.GetAllPrefixWords(q.front().second);
if ( word_prefix_pairs.size() != 0 ) {
for ( int i = 0; i < word_prefix_pairs.size(); ++i ) {
pair<string, string> p(q.front().first, word_prefix_pairs[i].second);
q.push(p);
}
}
q.pop();
}
}
cout << "max: " << max_val << " ctr: " << v.size() << endl;
return 0;
}