本文整理汇总了C++中Trie类的典型用法代码示例。如果您正苦于以下问题:C++ Trie类的具体用法?C++ Trie怎么用?C++ Trie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Trie类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: main
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
unsigned int N;
cin >> N;
cin.ignore();
vector<string> facilities = { " " };
vector<bool> foundFacilities = { false };
for (unsigned int i = 0; i < N; i++){
string facility;
getline(cin, facility);
facilities.emplace_back(facility);
foundFacilities.push_back(false);
}
std::sort(facilities.begin(), facilities.end());
Trie facilitiesTrie;
for (unsigned int i = 1; i <= N; i++){
facilitiesTrie.insert(i, facilities[i]);
}
//facilitiesTrie.print();
string text;
getline(cin, text);
auto& start = facilitiesTrie.root->nodes;
for (int i = 0, n = text.length(); i < n; i++){
auto result = start.find(tolower(text[i]));
if (result != start.end()){
TrieNode* cur = result->second;
if (cur->matchCode)
foundFacilities[cur->matchCode] = true;
int j = i + 1;
while (cur != nullptr){
auto result = cur->nodes.find(tolower(text[j++]));
if (result != cur->nodes.end()){
cur = result->second;
if (cur->matchCode)
foundFacilities[cur->matchCode] = true;
}
else
cur = nullptr;
}
}
}
for (int i = 1; i <= N; i++){
if (foundFacilities[i])
cout << facilities[i] << endl;
}
system("pause");
}
开发者ID:welcometors,项目名称:code-gems,代码行数:56,代码来源:Facilities+Extraction.cpp
示例17: main
int main(){
Trie t;
t.addWord("bel");
t.addWord("hbel");
t.addWord("hello");
t.addWord("hel");
return 0;
}
开发者ID:prateek27,项目名称:Coding-Problems,代码行数:10,代码来源:trie.cpp
示例18: main
int main()
{
Trie trie;
trie.init();
trie.loadDict("../dicts/segdict.gbk.v2.1");
//trie.loadDict("tmp");
cout<<trie.getMinLogFreq()<<endl;
trie.dispose();
return 0;
}
开发者ID:Alienfeel,项目名称:cppjieba,代码行数:10,代码来源:Trie.cpp
示例19: insert
void Trie::insert(const string &word) {
Trie * node = this;
int size = word.length();
for (int i = 0; i < size; i++) {
const char c = word[i];
if (node->children.find(c) == node->children.end())
node->add(c);
node = &node->children[c];
}
node->flag = true;
}
开发者ID:wasimk1995,项目名称:CacheSync,代码行数:11,代码来源:trie.cpp
示例20: loadData
/* Function to load all the words from the given ile in the Trie tree */
void
loadData(Trie &T, char *filename) {
ifstream file;
file.open(filename);
for (string word; getline(file, word);) {
T.stripWord(word);
T.insertWord(word);
}
file.close();
}
开发者ID:lea82,项目名称:Longest-Compound-Word,代码行数:12,代码来源:compoundWords.cpp
注:本文中的Trie类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论