trie字典树

匿名 (未验证) 提交于 2019-12-02 23:49:02

cat,cash,app,apple,aply,ok。

也许你的第一个想法是暴力搜索每一个单词,但是复杂度很很很很高啊

有一种东西叫做字典树,字典树的根节点连出来的每一条边存的就是出现的首字母,
然后但此后边的字母我们可以在后边存上, 这样每一个单词的最后一个单词就是最后存的
那条边,这样我们可以在那条代表着最后单词的边的下一个节点处打一个标记,然后表示一个单词就结束了;

like this

把每一个单词放到字典树中...

void add(string ch) {     int len = ch.size(), root = 0, x;     for (int i = 0; i < len; i++) {         x = ch[i] - 'a';         if (!trie[root][x]) trie[root][x] = ++tot;         tr[trie[root][x]] = 1;         root = trie[root][x];     }     tr[trie[root][x]] = 1; }
int query(string ch) {     int len = ch.size(), root = 0, ans = 0;     int x;     for (int i = 0; i < len; i++) {         x = ch[i] - 'a';         if (trie[root][x] == 0) return 0;         ans = tr[trie[root][x]];         tr[trie[root][x]] = 2;         root = trie[root][x];     }     return ans; }

上代码

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>  using namespace std; int tot, trie[400001][31], tr[400001]; string ch;  void add(string ch) {     int len = ch.size(), root = 0, x;     for (int i = 0; i < len; i++) {         x = ch[i] - 'a';         if (!trie[root][x]) trie[root][x] = ++tot;         tr[trie[root][x]] = 1;         root = trie[root][x];     }     tr[trie[root][x]] = 1; }  int query(string ch) {     int len = ch.size(), root = 0, ans = 0;     int x;     for (int i = 0; i < len; i++) {         x = ch[i] - 'a';         if (trie[root][x] == 0) return 0;         ans = tr[trie[root][x]];         tr[trie[root][x]] = 2;         root = trie[root][x];     }     return ans; }  int main() {     int n, m;     scanf("%d", &n);     for (int i = 1; i <= n; i++) {         cin>>ch;         add(ch);     }     scanf("%d", &m);     for (int i = 1; i <= m; i++) {         cin >> ch;         if (query(ch) == 1) printf("OK\n");         else if (query(ch) == 0) printf("WRONG\n");         else if (query(ch) == 2) printf("REPEAT\n");     } }

"strcmp()" Anyone?

C. Perfect Security

Remember the Word

转载请标明出处:trie字典树
文章来源: trie字典树
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!