"BZOJ1212" L language (AC automata)

Source: Internet
Author: User

"BZOJ1212" L language (AC automaton) surface

Bzoj

Exercises

Naturally, since you want to match the word, it's all thrown into the ac\ automaton.

Now think about how to match.
First \ (ac\) the normal match of the automaton
If this position is able to match the previous string
We need to figure out how this string is covered in this text string.
Whether its previous bit is exactly covered
If there is.
Nor can we directly calculate
Because it could be a different kind of string.
So, just open an array differential
Indicates that the current position can match

Finally, for each text string
Sweep past the difference score Group
Until there is \ (0\) the direct output is OK

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <set>#include <map>#include <vector>#include <queue>using namespaceStdinline intRead () {intx=0, t=1;CharCh=getchar (); while((ch<' 0 '|| Ch>' 9 ') &&ch!='-') Ch=getchar ();if(ch=='-') t=-1, Ch=getchar (); while(ch<=' 9 '&&ch>=' 0 ') x=x*Ten+ch-48, Ch=getchar ();returnX*t;}structnode{intvis[ -];intDep,lst,fail;} t[ the];intN,m,tot;Chars[2000000];intcc[2000000];BOOLvis[2000000];voidInsertChar*s) {intL=strlen (S+1), now=0; for(intI=1; i<=l;++i) {if(!t[now].vis[s[i]-97]) T[now].vis[s[i]-97]=++tot; Now=t[now].vis[s[i]-97];    T[now].dep=i; } t[now].lst=1;}voidGetfail () {queue<int> Q; for(intI=0;i< -; ++i)if(t[0].vis[i]) Q.push (t[0].vis[i]); while(! Q.empty ()) {intU=q.front (); Q.pop (); for(intI=0;i< -; ++i)if(T[u].vis[i]) T[t[u].vis[i]].fail=t[t[u].fail].vis[i],q.push (t[u].vis[i]);ElseT[u].vis[i]=t[t[u].fail].vis[i]; }}intMain () {n=read (); M=read (); for(intI=1; i<=n;++i) {scanf ("%s", S+1);    Insert (s); } getfail (); while(m--) {scanf ("%s", S+1);intL=strlen (S+1);intlst=0, now=0; Memset (CC,0,sizeof(cc)); memset (Vis,0,sizeof(VIS)); vis[0]=true; for(intI=1; i<=l;++i) {now=t[now].vis[s[i]-97]; for(intJ=now;j;j=t[j].fail)if(T[J].LST&AMP;&AMP;VIS[I-T[J].DEP]) {vis[i]=true; Cc[i-t[j].dep+1]++; Cc[i+1]--; }        } for(intI=1, tot=0; i<=l;++i) {tot+=cc[i];if(tot) ++lst;Else  Break; } printf ("%d\n", LST); }return 0;}

"BZOJ1212" L language (AC automata)

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.