本文整理汇总了C++中Trie类的典型用法代码示例。如果您正苦于以下问题:C++ Trie类的具体用法?C++ Trie怎么用?C++ Trie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Trie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: doIt
void doIt(){
int n, a[2002];
Trie* t = new Trie();
scanf("%d", &n);
Rep(i, n){
scanf("%d", &a[i]);
t->addWord(b2s(a[i], MAX_BIT));
}
示例2: find_shortest_prefix
string find_shortest_prefix(const string& s, const unordered_set<string>& D) {
// Build a trie according to given dictionary D
Trie T;
for (const string& word : D) {
T.insert(word);
}
return T.getShortestUniquePrefix(s);
}
示例3: main
int main(int argc, char const *argv[])
{
Trie trie;
trie.insert("ab");
trie.insert("door");
cout<<trie.startsWith("ke")<<endl;
return 0;
}
示例4: countWords
int countWords(const char *s) {
if(*s=='\0')
return words;
Trie *t = child[*s-'a'];
if(t==NULL)
return 0;
return t->countWords(s+1);
}
示例5: main
int main(){
Trie trie;
trie.insert("abcdefgh");
trie.insert("abdefg");
trie.insert("abdefg");
trie.insert("abdefg");
cout<<trie.prefix("abdee")<<endl;
}
示例6: countPreffixes
int countPreffixes(const char *s) {
if(*s=='\0')
return prefixes;
Trie *t = child[*s-'a'];
if(t==NULL)
return 0;
return t->countPreffixes(s+1);
}
示例7: main
int main()
{
Trie t;
cout<<(t.search("")?"true":"false");
t.insert("apple");
cout<<(t.startsWith("a")?"true":"false");
}
示例8: cantPrefFreeSubsets
long long cantPrefFreeSubsets(vector <string> words) {
Trie T;
for (int i=0; i<(int)words.size(); ++i)
T.insert(words[i]);
return calc(T);
}
示例9: main
int main(int argc, const char * argv[]) {
if(argc<2){
show_usage();
cerr << "not enought parameters!" << endl;
exit(1);
}
clock_t curr_time;
curr_time = clock();
// read from file
Trie* trie = new Trie();
cout << "Read from File" << endl;
string path = argv[1];
ifstream iFile(path);
if(!iFile) {
cerr << "unable to open file" << endl;
exit(1);
}
string line;
while (getline(iFile, line)) {
if (!line.empty() && line[line.size() - 1] == '\r')
line.erase(line.size() - 1);
trie->addWord(line);
}
curr_time = clock() - curr_time;
cout << "Read File and Build Trie Done! Time: " << ((float)curr_time)/CLOCKS_PER_SEC << " seconds" << endl;
// sortedSet stores all the compound words in decresing order in terms of string length
set<string, cmpStruct> sortedSet;
while(!trie->suffix_queue.empty()) {
StringPair sp = trie->suffix_queue.front();
trie->suffix_queue.pop();
if(trie->searchWord(sp.first, sp.second)) {
sortedSet.insert(sp.first);
}
}
curr_time = clock() - curr_time;
cout << "Search For Compound Words Done! Time: " << ((float)curr_time)/CLOCKS_PER_SEC << " seconds" << endl;
std::set<string>::iterator it;
int i = 0;
for (it = sortedSet.begin(); i < 2 && it != sortedSet.end(); ++it, ++i)
cout << "The " << i+1 << " Longest Compound Word:" << *it << endl;
cout << "Number of Total Compound Words: " << sortedSet.size() << endl;
//write all compound words to file
std::ofstream out("compoundWords.txt");
for (it = sortedSet.begin(); it != sortedSet.end(); ++it) {
out << *it << endl;
}
out.close();
delete trie;
return 0;
}
示例10: main
int main(int argc, char const *argv[])
{
Trie trie;
trie.insert("abc");
trie.insert("ab");
cout << trie.search("hel") << endl;
cout << trie.search("ab") << endl;
return 0;
}
示例11: main
int main()
{
Trie t;
string s("abcd");
string prefix("abc");
t.insert(s);
cout << t.search(s) << endl;
cout << t.startsWith(prefix) << endl;
}
示例12: isEmpty_
TrieChildren::TrieChildren(const char label, ITrie* child,
const std::string& word, const bool storeEmpty) : isEmpty_(storeEmpty)
{
memset(children_, 0, sizeof(children_));
children_[tolower(label) - 'a'] = child;
Trie t;
children_[tolower(word[0]) - 'a'] = t.Add(word.substr(1));
}
示例13: findBestRecipe
string findBestRecipe(const char *s) {
if(*s=='\0') {
if(best >= 0 && best < N) return dict[best];
return "NO";
}
Trie *t = child[(int)*s];
if(t==NULL) return "NO";
return t->findBestRecipe(s+1);
}
示例14: main
int main()
{
Trie obj = Trie();
obj.insert("");
obj.insert("search");
cout << obj.search("") << endl;
cout << obj.search("a") << endl;
return 0;
}
示例15: has_common_prefix
bool has_common_prefix(const std::vector<std::string>& values) {
Trie trie;
for (const auto& value : values) {
if (trie.add(value)) {
return true;
}
}
return false;
}