Http://acm.hdu.edu.cn/showproblem.php? PID = 2222 & AC Automation

Source: Internet
Author: User

The first AC automation question ~ It is actually the KMP algorithm on the dictionary tree. It is mainly used to learn how to build the dictionary tree and construct the failure pointer. The dictionary tree I used is an array simulation method ~

AC code:

#include <iostream>#include<string.h>#include<algorithm>#include<cstdio>#include<queue>#define Fp freopen("1.txt","r",stdin)#define N 2000005using namespace std;void in(int &a){    char ch;    while((ch=getchar())<'0'||ch>'9');    for( a=0;ch>='0'&&ch<='9';ch=getchar()) a=a*10+ch-'0';}typedef struct node{int next[26];int num;int fail;void init(){memset(next,-1,sizeof(next));num=0;fail=-1;}}Node;Node s[N];int p;char a[1000000];void insert(){int ind=0;for(int i=0;a[i];++i){int x=a[i]-'a';if(s[ind].next[x]==-1) {     s[++p].init();     s[ind].next[x]=p;     }ind=s[ind].next[x];}++s[ind].num;}void AC(){queue<int>Q;int root=0;s[root].fail=-1;Q.push(root);while(!Q.empty()){int t=Q.front();Q.pop();for(int i=0;i<26;++i){int ind=s[t].next[i];if(ind!=-1){if(t==root) s[ind].fail=root;else{  int p=s[t].fail;  while(p!=-1)  {  if(s[p].next[i]!=-1)  {  s[ind].fail=s[p].next[i];  break;  }  p=s[p].fail;  }  if(p==-1) s[ind].fail=root;  }Q.push(ind);}    }}}int  Find(){    int root=0;     int ct=0;    int res=root;    for(int i=0;a[i];++i)    {        int x=a[i]-'a';        while(res!=root&&s[res].next[x]==-1) res=s[res].fail;        res=s[res].next[x];        if(res==-1) res=root;        int ans=res;        while(ans!=root&&s[ans].num>0)        {            ct+=s[ans].num;            s[ans].num=-1;            ans=s[ans].fail;       }    }    return ct;}int main(){   Fp;   int T;   in(T);   while(T--)   {       s[0].init();       int n;       //scanf("%d",&n);       in(n);       //gets(a);       while(n--)       {           gets(a);           insert();       }       gets(a);       AC();       printf("%d\n",Find());   }return 0;}

Refer:

Http://blog.csdn.net/niushuai666/article/details/7002823

Http://www.cnblogs.com/zhuangli/archive/2008/08/13/1267249.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.