ZOJ3228 searching the String (AC automaton)

Source: Internet
Author: User

The topic is probably given to a main string, asking for several occurrences of a pattern string, some of which require no overlap.

For overlapping is a straightforward multi-pattern matching problem, but not overlapping, in the matching process greedy record the current matching of the main string position, and then whenever a new match according to the location of the record to determine whether the new match is established, the last update location.

In addition, given that the data can occur in multiple pattern strings the same situation, the implementation to do some processing:

1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 #defineMAXN 6666667 //-1 None, 0 overlap, 1 not, 2 all8 inttn,ch[maxn][ -],FAIL[MAXN],FLAG[MAXN],LEN[MAXN];9 intInsertChar*s,intk) {Ten     intx=0, l=0; One      for(intI=0; T[n]; ++i) { A         inty=s[i]-'a'; -         if(ch[x][y]==0) ch[x][y]=++tn; -x=Ch[x][y]; the++l; -     } -len[x]=l; -     if(flag[x]==-1) flag[x]=K; +     Else if(flag[x]==0&& k==1) flag[x]=2; -     Else if(flag[x]==1&& k==0) flag[x]=2; +     returnx; A } at voidGetfail () { -memset (fail,0,sizeof(flag)); -queue<int>que; -      for(intI=0; i< -; ++i) { -         if(ch[0][i]) Que.push (ch[0][i]); -     } in      while(!Que.empty ()) { -         intx=Que.front (); Que.pop (); to          for(intI=0; i< -; ++i) { +             if(Ch[x][i]) { - Que.push (Ch[x][i]); thefail[ch[x][i]]=Ch[fail[x]][i]; *}Elsech[x][i]=Ch[fail[x]][i]; $         }Panax Notoginseng     } - } the intlast[maxn],ans[2][maxn],first[111111],second[111111]; + Charstr[111111]; A voidquery () { the     intx=0; +      for(intI=0; Str[i]; ++i) { -         inty=str[i]-'a'; $         inttmp=x=Ch[x][y]; $          while(TMP) { -             if(flag[tmp]!=-1&& flag[tmp]!=1) ++ans[0][tmp]; -             if(flag[tmp]!=-1&& flag[tmp]!=0){ the                 if(last[tmp]==-1|| i-last[tmp]>=Len[tmp]) { -last[tmp]=i;Wuyi++ans[1][tmp]; the                 } -             } Wutmp=fail[tmp]; -         } About     } $ } - intMain () { -     intN,cse=0; -     Chars[ One]; A      while(~SCANF ("%s", str)) { +tn=0; thememset (CH,0,sizeof(CH)); -memset (flag,-1,sizeof(flag)); $scanf"%d",&n); the          for(intI=0; i<n; ++i) { thescanf"%d%s",&first[i],s); thesecond[i]=Insert (S,first[i]); the         } - Getfail (); inmemset (ans,0,sizeof(ans)); thememset (last,-1,sizeof(last)); the query (); Aboutprintf"Case %d\n",++CSE); the          for(intI=0; i<n; ++i) { theprintf"%d\n", Ans[first[i]][second[i]]); the         } +Putchar ('\ n'); -     } the     return 0;Bayi}

ZOJ3228 searching the String (AC automaton)

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.