"BZOJ2780" "Spoj8093" Sevenk love oimaster suffix automata

Source: Internet
Author: User

#include <stdio.h>int main () {puts ("reprint please specify source thank you");p UTS ("http://blog.csdn.net/vmurder/article/details/43015849 ");}

Test instructions

Nm

N A string

M-String

In the example, you should throw the next line in the penultimate row.


Ask the M-string for each of the first n strings in which several occurrences have occurred.


Exercises

First of all, this problem

"BZOJ2754", "SCOI2012", the name on the Meow planet.

Is the same, just a bit more card, or meow data is too weak.


Although this problem is a suffix automaton, but the general idea and

"BZOJ2434" "NOI2011" Ali typewriter AC automatic machine

is the same. DFS sequence + Tree array optimization


OK, say the question.

Is that we build the generalized suffix tree "is multi-string reverse-order suffix automata"

Then, based on the DFS sequence, the tree array is used to maintain the number of different strings in a certain interval.

I'll stick to the two detailed URL.

16bitwar:http://blog.csdn.net/jiangyuze831/article/details/42964105

Wyfcyx:http://wyfcyx.is-programmer.com/posts/76391.html

Code:

#include <map> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm  > #define N 201000#define T 26using namespace std;struct suffix_tree//generalized suffix tree {int head[n],next[n],v[n],cnt;void add (int U,int _v) {v[++cnt]=_v;next[cnt]=head[u];head[u]=cnt;} int in[n],out[n],dfn,stk[n];void BUILD_DFN (int x) {in[x]=++dfn;stk[dfn]=x;for (int i=head[x];i;i=next[i]) BUILD_DFN (v [i]); O UT[X]=DFN;}} Sft,belong;struct Query{int id,p; QUERY (int _id=0,int _p=0): ID (_id), p (_p) {}bool operator < (const QUERY &a) Const{return SFT.OUT[P]&LT;SFT.OUT[A.P ];}} Ask[n];int Vis[n],ans[n];char s[n];int n,m;struct fenwick{int a[n],cnt[n];void Add (int p,int x) {for (;p <n;p+=p&- p) a[p]+=x;} int query (int p) {int ans=0,temp=p;for (p=sft.out[temp];p; p-=p&-p) ans+=a[p];for (p=sft.in[temp]-1;p;p-=p&-p) Ans-=a[p];return ans;}} Fw;struct SAM//generalized suffix automaton {map<int,int>next[n];int pa[n],deep[n];int root,last,cnt;int newnode (int dep) {deep[++ Cnt]=dep;return CNT;} void Init () {root=last=Cnt=1;} void Add (char alp,int ID)//Establish generalized suffix automaton {int np=next[last][alp];if (NP) {if (deep[np]==deep[last]+1) last=np;else {int nq= NewNode (deep[last]+1);p a[nq]=pa[np],pa[np]=nq;next[nq]=next[np];int p=last;while (P&AMP;&AMP;NEXT[P][ALP]==NP) NEXT[P][ALP]=NQ,P=PA[P];LAST=NQ;}} else {np=newnode (deep[last]+1); int p=last;while (P&&!next[p][alp]) next[p][alp]=np,p=pa[p];if (!p) pa[np]= root;else {int q=next[p][alp];if (deep[q]==deep[p]+1) pa[np]=q;else {int Nq=newnode (deep[p]+1);p A[nq]=pa[q],pa[q]=pa [Np]=nq;next[nq]=next[q];while (P&AMP;&AMP;NEXT[P][ALP]==Q) next[p][alp]=nq,p=pa[p];}} LAST=NP;} Belong.add (Last,id);} void build_sft () {for (int i=2;i<=cnt;i++) Sft.add (pa[i],i);} void Deal_query () {bool Find;int i,j,p;for (int i=1;i<=m;i++) {scanf ("%s", s), Find=1;for (p=root,j=0;s[j];j++) {if (! NEXT[P][S[J]]) {find=0;break;} P=NEXT[P][S[J]];} if (find) ask[i]=query (i,p); else Ask[i]=query (i,-1);} Sort (ask+1,ask+m+1);}} Sam;int Main () {//freopen ("test.in", "R", stdin); int i,j,k;scanf ("%d%d", &n,&m); Sam.init (); for (I=1;i<=n;i++) {scanf ("%s", s), Sam.last=sam.root;for (j=0;s[j];j++) Sam.add (s[j],i);} Sam.build_sft (); SFT.BUILD_DFN (1); Sam.deal_query (); for (k=1;k<=m&&ask[k].p==-1;k++); for (j=1;j<= sam.cnt;j++) {int u=sft.stk[j];for (i=belong.head[u];i;i=belong.next[i]) {int V=belong.v[i];fw.add (j,1), if (Vis[v]) Fw.add (vis[v],-1); vis[v]=j;} while (sft.out[ask[k].p]==j) {ans[ask[k].id]=fw.query (ASK[K].P); k++;}} for (i=1;i<=m;i++) printf ("%d\n", Ans[i]); return 0;}



"BZOJ2780" "Spoj8093" Sevenk love oimaster suffix 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.