"BZOJ3172" "Tjoi2013" word ac automaton template problem

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/vmurder/article/details/42711351
In fact, I just feel that the original traffic is a bit more uncomfortable than unauthorized piracy 233 ...

The water explosion, direct AC automatic machine blindly write on the line.

Keng:...... After 1.5 months of moving AC, unexpectedly because the problem can have repeated word 233.


Code:

#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define N 1001000#define M 205#define T 27#define inf 0x3f3f3f3fusing namespace std;struct ksd{int v,next;} E[m];int head[n],tot;inline void Add (int u,int v) {E[++tot].v=v;e[tot].next=head[u];head[u]=tot;} Char S[n];int begin[m],end[m],n; Left closed right open int ans[n];struct acauto{int next[n][t],fail[n],flag[n],cnt;void Insert (int id,int l,int r) {int i,x,alp;for (x=0, i=l;i<=r;i++) {alp=s[i]-' a '; if (!next[x][alp]) Next[x][alp]=++cnt;x=next[x][alp];} Add (x,id);} int Stk[n],top;void build () {Queue<int>q;q.push (0); int i,u,v;while (!q.empty ()) {U=q.front (), Q.pop (); stk[++top ]=u;for (i=0;i<t;i++) {if (V=next[u][i]) {if (!u) Fail[v]=0;else Fail[v]=next[fail[u]][i];q.push (v);} else next[u][i]=next[fail[u]][i];}}} int f[n];void find () {int i,x,alp;for (i=0;i<end[n];i++) {if (s[i]== ' $ ') {x=0;continue;} alp=s[i]-' a '; f[x=next[x][alp]]++;}} void DP () {int I,u,v;while (top) {u=stk[top--];for (i=head[u];i;i=e[I].next) ans[e[i].v]+=f[u];f[fail[u]]+=f[u];}} Acauto;int Main () {//freopen ("test.in", "R", stdin), int i,j,k;scanf ("%d", &n), End[0]=-1;for (i=1;i<=n;i++) {s[+ +end[i-1]]= ' $ '; begin[i]=end[i-1]+1;scanf ("%s", S+begin[i]); End[i]=end[i-1]+strlen (S+begin[i]); Acauto.insert (I, Begin[i],end[i]);} End[n]++;acauto.build (); Acauto.find (); ACAUTO.DP (); for (i=1;i<=n;i++) printf ("%d\n", Ans[i]); return 0;}


"BZOJ3172" "Tjoi2013" word ac automaton template problem

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.