本文共 691 字,大约阅读时间需要 2 分钟。
模板题
#include#include #include #include #include #include using namespace std;struct node{ int cnt; struct node *next[26]; node() { cnt = 0; memset(next,0,sizeof(next)); }};node *root = NULL;void buildtrie(char *s){ node *p = root; node *tmp = NULL; int len = strlen(s); for(int i = 0; i <= len - 1; i++) { if(p->next[s[i] - 'a'] == NULL) { tmp = new node; p->next[s[i] - 'a'] = tmp; } p = p->next[s[i] - 'a']; p->cnt ++; }}void findtrie(char *s){ node *p = root; int len = strlen(s); for(int i = 0; i <= len - 1; i++) { if(p->next[s[i] - 'a'] == NULL) { cout << 0 << endl; return ; } p = p -> next[s[i]-'a']; } cout << p->cnt <
转载地址:http://fnwtb.baihongyu.com/