当前位置: 首页>>代码示例>>C++>>正文


C++ Trie类代码示例

本文整理汇总了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));
    }
开发者ID:mkiken,项目名称:CompetitiveProgramming,代码行数:8,代码来源:trie.cpp

示例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);
}
开发者ID:stratwine,项目名称:epi,代码行数:8,代码来源:Shortest_unique_prefix.cpp

示例3: main

int main(int argc, char const *argv[])
{
    Trie trie;
    trie.insert("ab");
    trie.insert("door");
    cout<<trie.startsWith("ke")<<endl;
    return 0;
}
开发者ID:goldslime88,项目名称:leetcode,代码行数:8,代码来源:Implement_Trie_(Prefix_Tree).cpp

示例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);
 }
开发者ID:pavelsimo,项目名称:ProgrammingContest,代码行数:8,代码来源:E.cpp

示例5: main

int main(){
	Trie trie;
	trie.insert("abcdefgh");
	trie.insert("abdefg");
	trie.insert("abdefg");
	trie.insert("abdefg");
	cout<<trie.prefix("abdee")<<endl;
}
开发者ID:fenghuo,项目名称:coding,代码行数:8,代码来源:A_Trie.cpp

示例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);
 }
开发者ID:pavelsimo,项目名称:ProgrammingContest,代码行数:8,代码来源:E.cpp

示例7: main

int main()
{
	Trie t;
	cout<<(t.search("")?"true":"false");
	t.insert("apple");
	cout<<(t.startsWith("a")?"true":"false");
	
}
开发者ID:MastarB,项目名称:leetcode,代码行数:8,代码来源:ImplementTrie(PrefixTree).cpp

示例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);
	}
开发者ID:alxsoares,项目名称:contest-problem-solutions,代码行数:8,代码来源:PrefixFreeSubsets.cpp

示例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;
}
开发者ID:ShanshanHe,项目名称:LeetCode,代码行数:58,代码来源:main.cpp

示例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;
}
开发者ID:zrss,项目名称:LeetCode,代码行数:9,代码来源:ImplementTrie.cpp

示例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;
}
开发者ID:zzfan,项目名称:leetCode,代码行数:9,代码来源:trie.cpp

示例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));
}
开发者ID:mwaghmar,项目名称:try,代码行数:9,代码来源:TrieChildren.cpp

示例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);
 }
开发者ID:pavelsimo,项目名称:ProgrammingContest,代码行数:9,代码来源:TWSTR_BACKUP.cpp

示例14: main

int main()
{
    Trie obj = Trie();
    obj.insert("");
    obj.insert("search");
    cout << obj.search("") << endl;
    cout << obj.search("a") << endl;
    return 0;
}
开发者ID:applefishsky009,项目名称:LeetCode,代码行数:9,代码来源:208-ImplementTrie(PrefixTree).cpp

示例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;
}
开发者ID:flaub,项目名称:practice,代码行数:9,代码来源:trie.hpp


注:本文中的Trie类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。