HDU 2222 AC_Automation first AC automatic machine

Source: Internet
Author: User

I spent a whole day and a half working on AC automation, my first AC automation.

Debug n has been reading the question for a long time!

Question:

Given N words and a mode string, calculate the number of occurrences in the word mode string.

To solve the problem, first construct a dictionary tree and then perform an AC automatic machine on the dictionary tree.

I DEBUG N for a long time... the original AC automatic machines are constructed right!

Note.

1. Words can be repeated.

2. Just ask how many words appear in the mode string at most, which is N words at most.

Continue to switch the question of the AC automatic machine. The last article is a summary.

#include<iostream>#include<cstdio>#include<string.h>#define MAX 26using namespace std;int root,tot;struct node{       int fail;       int cnt;       int next[MAX];       void init()   {            memset( next,0,sizeof(next) );            fail=-1;cnt=0;       }}Tire[5555555];int queue[5555555];void init(){     root=tot=0;     Tire[root].init();}void insert( int root,char *s ){     int p=root;     int i=0,k;     while( s[i] ) {            k=s[i++]-'a';            if( !Tire[p].next[k] )            {                Tire[++tot].init();                Tire[p].next[k]=tot;            }            p=Tire[p].next[k];     }     Tire[p].cnt++;}void build_ac_automation(){     int head,tail;     head=tail=0;     queue[tail++]=root;     while( head<tail ) {            int cur=queue[head++];            for( int i=0;i<MAX;i++ ){                 if( Tire[cur].next[i] ) {                     int son=Tire[cur].next[i];                     int p=Tire[cur].fail;                     if( cur==root )                         Tire[son].fail=root;                     else                         Tire[son].fail=Tire[p].next[i];                     queue[tail++]=son;                 }                 else {                     int p=Tire[cur].fail;                     if( cur==root )                         Tire[cur].next[i]=root;                     else                         Tire[cur].next[i]=Tire[p].next[i];                 }            }     }}int query( char *s ){    int i=0,k,p=root;    int ret=0;    while( s[i] )    {           k=s[i++]-'a';           while( !Tire[p].next[k]&&p!=root )               p=Tire[p].fail;           p=Tire[p].next[k];           if(p==-1) p=0;           int temp=p;           while( temp!=root&&Tire[temp].cnt!=-1 )           {                  ret+=Tire[temp].cnt;                  Tire[temp].cnt=-1;                  //sTire[temp].cnt=0;                  temp=Tire[temp].fail;           }    }    return ret;}char str[1111111];int main(){    int T;    scanf( "%d",&T );    while( T-- ){           init();           int N;           scanf( "%d",&N );           while( N-- )           {                 scanf( "%s",&str );                 insert( root,str );           }           build_ac_automation();           scanf( "%s",&str );           printf( "%d\n",query(str) );           //system("pause");    }    return 0;}

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.