这题模板题.............但是竟然要去重........调试了半天才发现....................
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> using namespace std; struct trie { trie *next[128]; int flag; int num; trie *fail; trie() { fail = NULL; flag = num = 0; memset(next,0,sizeof(next)); } }*q[511111]; trie *rt = new trie(); int vi[555],cnt,head,tail; char keyword[222]; char book[11111]; void insert(char *key,int num) { trie *p = rt; while(*key) { int t = int(*key); if(p->next[t] == NULL) p->next[t] = new trie(); p = p->next[t]; key ++; } p->flag = 1; p->num = num; } void bfs() { rt->fail = NULL; head = tail = 0; q[head++] = rt; while(head != tail) { trie *t = q[tail++]; trie *tmp = NULL; for(int i=0; i<128; i++) { if(t->next[i] != NULL) { if(t == rt ) t->next[i]->fail = rt; else { tmp = t->fail; //沿着父亲的fail指针走 while(tmp != NULL) { if(tmp->next[i] != NULL) { t->next[i]->fail = tmp->next[i]; break; } tmp = tmp->fail; } if(tmp == NULL) t->next[i]->fail = rt; } q[head++] = t->next[i]; } } } } void query(char *key) { trie *p = rt; cnt = 0; int ok = 0; while(*key) { int t = int(*key); while(p != rt && p->next[t] == NULL) p = p->fail; p = p->next[t]; if(p == NULL) p = rt; trie *tmp = p; while(tmp != rt && tmp->flag != 0) { vi[cnt] = tmp->num; cnt += tmp->flag; tmp = tmp->fail; } key++; } } int main() { int n,m; cin >> n; getchar(); for(int i=0; i<n; i++) { gets(keyword); insert(keyword,i+1); } bfs(); int ans = 0; cin >> m; getchar(); for(int i=1; i<=m; i++) { gets(book); memset(vi,0,sizeof(vi)); cnt = 0; query(book); if(cnt != 0) { ans ++; printf("web %d:",i); sort(vi,vi+cnt); int dd = unique(vi , vi + cnt) - vi; for(int j=0; j<dd; j++) printf(" %d",vi[j]); puts(""); } } printf("total: %d ",ans); return 0; }