"NOI2015" Tasting meeting SAM

Source: Internet
Author: User

The harm of not reading the problem, and the pretreatment does not do the harm of the number. Two hours have passed ...... ..... ..... .........................

Not hard. The suffix machine is built in reverse order, and the suffix tree is obtained by using the fail edge. (In fact, there's no running on the tree.) )

Because the string represented by each leaf node on the suffix tree is the suffix of the original string. Now after the reverse order, it becomes a prefix. That

Because the SZ array of a node x indicates that all the strings represented by this node appear sz[x] times. And the string is represented by dep[fa[x]] [1 ~ dep[x] So theoretically considering a node's contribution to the answer is to add one to dep[fa[x]+1] ~ dep[x. But obviously there is a prefix and the idea, then it is not.

Word. The idea of a suffix automaton is to use a long list of information that is already there, and its suffix to summarize information. And then all the information is drawn.

(in particular, for arbitrary1≤P,Q≤N1≤p,q≤n,P≠QP≠Q, sectionp p  Cup wine and   q "> q Q Cup wine is " 0 ">< Span id= "mathjax-span-177" class= "Mrow" > 0 similar ". I don't know if anyone has read the same question as I did. And some of the MAXL,MINL did not initialize, WA for a long time. The heart is so tired.

1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineRep (i,j,k) for (register int i = j; I <= K; i++)5 #defineDow (I,J,K) for (register int i = j; I >= K; i--)6 #defineMAXN 3023337 #definell Long Long8 #defineINF 0x7fffffff9 using namespacestd;Ten  OneInlineintRead () { A     ints =0, t =1;Charc =GetChar (); -      while(!isdigit (c)) {if(c = ='-') T =-1; c =GetChar ();} -      while(IsDigit (c)) s = S *Ten+ C- -, C =GetChar (); the     returnS *T;  - } -  - intU, p, last =1, tot =1, V, NV, dep[maxn<<1], fa[maxn<<1], sz[maxn<<1];  + intmaxl[maxn<<1][2], minl[maxn<<1][2], son[maxn<<1][ -]; -InlinevoidExtendintCintkey) { +U = Last; last = P = ++tot; DEP[P] = Dep[u] +1; SZ[P] =1; Amaxl[p][0] = minl[p][0] = key;//cout<< "B" <<p<< "" <<key<<endl; atmaxl[p][1] = -2e9; minl[p][1] =2e9; -      while(U &&!son[u][c]) son[u][c] = p, u =Fa[u]; -     if(!u) Fa[p] =1; -     Else { -v =Son[u][c]; -         if(Dep[v] = = Dep[u] +1) Fa[p] =v; in         Else { -NV = ++tot; DEP[NV] = Dep[u] +1; toFA[NV] = Fa[v]; FA[V] = fa[p] =NV; +maxl[tot][0] = maxl[tot][1] = -2e9; -minl[tot][0] = minl[tot][1] =2e9; thememcpy (Son[nv],son[v],sizeof(Son[v])); *              while(Son[u][c] = = v) son[u][c] = NV, u =Fa[u]; $         }Panax Notoginseng     } - } the  + intq[maxn<<1], tong[maxn<<1] = {0}, KEY[MAXN]; AInlinevoidPre () { theRep (I,1, tot) tong[dep[i]]++; +Rep (I,1, tot) tong[i] + = tong[i-1]; -Dow (I,tot,1) q[tong[dep[i]]--] =i; $     intx; $Dow (I,tot,1) x = Q[i], sz[fa[x] + =Sz[x]; -dep[0] = -1;  - }  thell CNT[MAXN], ANS[MAXN];CharC[MAXN]; -InlinevoidUpdatemax (intXintv) {Wuyi //cout<< "M" <<x<< "<<v<<" "<<maxl[x][1]<<" "; themaxl[x][1] = max (maxl[x][1],v); - //cout<<maxl[x][1]<<endl; Wu     if(maxl[x][1] > maxl[x][0]) Swap (maxl[x][1],maxl[x][0]); - } AboutInlinevoidUpdatemin (intXintv) { $ //cout<< "I" <<x<< "<<v<<" "<<minl[x][1]<<" "; -minl[x][1] = min (minl[x][1],v); - //cout<<minl[x][1]<<endl; -     if(minl[x][1] < minl[x][0]) Swap (minl[x][1],minl[x][0]); A } +  the intMain () { -     intn = read (); scanf"%s", c+1); $maxl[1][0] = maxl[1][1] = -2e9; minl[1][0] = minl[1][1] =2e9; theRep (I,1, n) key[i] =read (); theDow (I,n,1) Extend (c[i]-'a', Key[i]); thePre ();intx; thememset (ans, -,sizeof(ans)); -memset (CNT,0,sizeof(CNT)); inDow (I,tot,1) { thex =Q[i]; the         if(Sz[x] >1 ) { AboutCNT[DEP[X]] + = 1LL * (Sz[x]-1) * Sz[x]/2; the             if(Dep[fa[x]] >=0) Cnt[dep[fa[x]] = 1ll * (Sz[x]-1) * Sz[x]/2; theANS[DEP[X]] = max (Ans[dep[x]],max (1ll*maxl[x][0]*maxl[x][1],1ll*minl[x][0]*minl[x][1])); the         }  +Updatemax (fa[x],maxl[x][0]); -Updatemax (fa[x],maxl[x][1]); theUpdatemin (fa[x],minl[x][0]);BayiUpdatemin (fa[x],minl[x][1]); the     }  theDow (i,n-1,0) Cnt[i] + = cnt[i+1];  -Dow (i,n-1,0) Ans[i] = max (ans[i],ans[i+1]);  -Rep (I,0, N-1)if(!cnt[i]) ans[i] =0; theRep (I,0, N-1) printf ("%lld%lld\n", Cnt[i],ans[i]);  the  //x = read (); the     return 0; the}

"NOI2015" Tasting meeting SAM

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.