重新做了下hdu2222,结果WA。。后来发现加数据了,有重复单词,感觉这种做法真无聊。
贴下模版部分:
struct node { int ct; node *pre, *next[26]; void init() { ct = 0; pre = 0; memset(next, 0, sizeof(next)); }};int cnt; //节点总数node *root, trie[500010];node *queue[500010];void insertStr(node *root, char *str) { int index, len = strlen(str); for(int i = 0; i < len; i++) { index = str[i]-'a'; if(root->next[index] == 0) { root->next[index] = &trie[++cnt]; root->next[index]->init(); } root = root->next[index]; } root->ct++;}void buildAC() { int head, tail; node *u, tmp; queue[0] = root; root->pre = root; head = tail = 0; while(head <= tail) { u = queue[head++]; for(int i = 0; i < 26; i++) { if(u->next[i] == 0) { if(u == root) u->next[i] = root; else u->next[i] = u->pre->next[i]; } else { if(u == root) u->next[i]->pre = root; else u->next[i]->pre = u->pre->next[i]; queue[++tail] = u->next[i]; } } }}