HDU_Keywords Search
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<queue> using namespace std; char a[1000001],p[55]; struct Trie { Trie *child[26]; Trie *fail; //失败指针 //int num; int cnt; //由于同一单词可能出现多次。。。 Trie() { //num=0; cnt=0; fail=0; memset(child,0,sizeof(child)); } }; Trie *root,*s,*lrelia; void Create(char *str) { s=root; int i=0; while(str[i]) { int id=str[i]-'a'; if(s->child[id]==0) { s->child[id]=new Trie; } s=s->child[id]; i++; } s->cnt++; } void makeFail() //一个节点一个节点找fail指针 { Trie *front ; queue<Trie *>q ; q.push(root) ; while(!q.empty()){ front = q.front() ; q.pop() ; for(int i = 0;i < 26;i++){ if(front->child[i] != NULL){ //父结点有孩子i,则找孩子i的fail指针 if(front == root) front->child[i]->fail = root ;//与根结点相连的结点的fail指针都指向根结点 else{ Trie *temp = front ; while(temp->fail != NULL){ //父结点fail指针非空 if(temp->fail->child[i] != NULL){ //父结点fail指针指向的结点有孩子i front->child[i]->fail = temp->fail->child[i] ; break ; } temp = temp->fail ;//父结点向上转移 } if(temp->fail == NULL) front->child[i]->fail = root ; } q.push(front->child[i]) ;//找到孩子i的fail指针后将孩子i加入队列 } } } } int search(char *str) { Trie *p = root ; Trie *temp = NULL ; int i=0,k,ans = 0 ; while(str[i]){ k=str[i] - 'a' ; while(p != root && p->child[k] == NULL){ p = p->fail ; } if(p->child[k] != NULL){//p记录当前位置最长的后缀匹配,下次从该支继续匹配 p = p->child[k] ; temp = p ; //用temp继续找当前位置较短的后缀匹配 while(temp != root && temp->cnt!=0){ ans +=temp->cnt; temp->cnt = 0 ; temp = temp->fail ; } } i++; } return ans; } int main() { int t,k,i,ans; scanf("%d",&t); while(t--) { root=new Trie; scanf("%d",&k); getchar(); ans=0; while(k--) { scanf("%s",p); Create(p); //创造字典树 } makeFail(); scanf("%s",a); ans=search(a); printf("%d\n",ans) ; delete root; } return 0; }
静态模板:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<queue> using namespace std; char a[1000001],str[55]; struct Trie { int child[26]; int cnt; int fail; Trie() { cnt=0; fail=-1; memset(child,0,sizeof(child)); } void set() { cnt=0; fail=-1; memset(child,0,sizeof(child)); } }t[240005]; int p; void Create(char *s) { int root=0,i=0,id; while(s[i]) { id=s[i]-'a'; if(t[root].child[id]==0) t[root].child[id]=p++; root=t[root].child[id]; i++; } t[root].cnt++; } void makeFail() { queue<int> q; int root=0,front; q.push(root); while(!q.empty()) { front=q.front(); q.pop(); for(int i=0;i<26;++i) { if(t[front].child[i]!=0) { if(front==root) t[t[front].child[i]].fail=root; else { int temp=front; while(t[temp].fail!=-1) { if(t[t[temp].fail].child[i]!=0) { t[t[front].child[i]].fail=t[t[temp].fail].child[i]; break; } temp=t[temp].fail; } if(t[temp].fail==-1) t[t[front].child[i]].fail=0; } q.push(t[front].child[i]); } } } } int Search(char *s) { int p=0,temp=0; int i=0,k,ans=0; while(s[i]) { k=s[i]-'a'; while(p!=0&&t[p].child[k]==0) p=t[p].fail; if(t[p].child[k]!=0) { p=t[p].child[k]; temp=p; while(temp!=0&&t[temp].cnt!=0) { ans+=t[temp].cnt; t[temp].cnt=0; temp=t[temp].fail; } } i++; } return ans; } int main() { int T,k,i; scanf("%d",&T); while(T--) { p=1; scanf("%d",&k); getchar(); while(k--) { scanf("%s",str); Create(str); //创造字典树 } makeFail(); scanf("%s",a); printf("%d\n",Search(a)) ; for(i=0;i<p;++i) t[i].set(); } return 0; }