Cf 235c semantic ical Quest (SAM)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: Give a string of S, a string of T. How many strings in s can be obtained through "processing" T.

Processing refers to dividing the string into two parts and changing the order

Http://codeforces.com/contest/235/problem/C
CLJ question, Tat ~~~~~~~~~~

This process is a little troublesome, but we can splice two T strings.

Then all the substrings in ''are processed by T.

Create a Sam for the s string and run t' again to match it.

If the matching length is greater than | T |, You need to judge that the maximum matching length of a suffix at the current position is greater than | T |. This can be achieved directly through the PRE pointer. The string accepted by the PRE pointer is the suffix of the current position, or satisfied.

But pay attention to deduplication. The same substring in ''cannot be run multiple times. Therefore, add a flag to Sam.

Previously, the topology pre-processes the number of nodes that reach the final state.

This is the official answer of CLJ.

This problem can be solved by using suffix structures.

I think suffix automaton is the best way to solve it because it is simple and clear.

So let us build a suffix Automaton of the input string S.

And consider the query string X.

Let us build a string t to be X concatenate X and drop the last char.

So every consecutive sub-string of T with length | x | is a rotation of X.

Let us read string T with suffix automaton we 've build, and every time take the first Char out and add a new char, add the answer by the number of string equal to this current sus-btring of T (which is a rotation of X ).

And one more thing, We shoshould consider the repetend of X as well,

#include<iostream>  #include<cstdio>  #include<map>  #include<cstring>  #include<cmath>  #include<vector>  #include<algorithm>  #include<set>  #include<string>  #include<queue>  #define inf 1600005  #define M 40  #define N 1100001#define maxn 2000005  #define eps 1e-7#define zero(a) fabs(a)<eps  #define Min(a,b) ((a)<(b)?(a):(b))  #define Max(a,b) ((a)>(b)?(a):(b))  #define pb(a) push_back(a)  #define mp(a,b) make_pair(a,b)  #define mem(a,b) memset(a,b,sizeof(a))  #define LL long long  #define MOD 1000000007#define lson step<<1#define rson step<<1|1#define sqr(a) ((a)*(a))  #define Key_value ch[ch[root][1]][0]  #define test puts("OK");  #define pi acos(-1.0)#define lowbit(x) ((x)&(-(x)))#pragma comment(linker, "/STACK:1024000000,1024000000")  #define vi vector<int> using namespace std; struct SAM{    SAM *pre,*son[26];    int len,cnt,mark;}*root,*tail,que[N*2],*b[N*2];int tot;void add(int c,int l){    SAM *np=&que[tot++],*p=tail;    tail=np;np->len=l;    while(p&&p->son[c]==NULL) p->son[c]=np,p=p->pre;    if(p==NULL) np->pre=root;    else{        SAM *q=p->son[c];        if(p->len+1==q->len) np->pre=q;        else{            SAM *nq=&que[tot++];            *nq=*q;            nq->len=p->len+1;            np->pre=q->pre=nq;            while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre;        }    }}char str[N];int cnt[N];void slove(){    mem(cnt,0);    int l=strlen(str);    for(int i=0;i<tot;i++) cnt[que[i].len]++;    for(int i=1;i<=l;i++) cnt[i]+=cnt[i-1];    for(int i=0;i<tot;i++) b[--cnt[que[i].len]]=&que[i];    SAM *p=root;    for(int i=0;i<l;i++) (p=p->son[str[i]-'a'])->cnt++;      for(int i=tot-1;i>0;i--){          if(b[i]->pre) b[i]->pre->cnt+=b[i]->cnt;      }  }int main(){    //freopen("input.txt","r",stdin);    //freopen("output.txt","w",stdout);    while(scanf("%s",str)!=EOF){        tot=0;        root=tail=&que[tot++];        for(int i=0;str[i];i++)            add(str[i]-'a',i+1);        slove();root->cnt=0;        //for(int i=0;i<tot;i++) printf("%d\n",que[i].cnt);        int q,mark=0;        scanf("%d",&q);        while(q--){            mark++;            int ans=0;            SAM *p=root;            scanf("%s",str);            int l=strlen(str),len=0;            for(int i=0;i<2*l;i++){                int c=str[i<l?i:i-l]-'a';                if(p->son[c]) len++,p=p->son[c];                else{                    while(p&&p->son[c]==NULL) p=p->pre;                    if(p==NULL) len=0,p=root;                    else len=p->len+1,p=p->son[c];                }                if(len>=l){                    while(p->pre&&p->pre->len>=l) len=p->pre->len,p=p->pre;                    if(p->mark!=mark){                        p->mark=mark;                        ans+=p->cnt;                    }                }            }            printf("%d\n",ans);        }        for(int i=0;i<tot;i++){            mem(que[i].son,NULL);            que[i].pre=NULL;            que[i].mark=0;        }    }    return 0;}

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.