codeforces 235 C Cyclical Quest (尾碼自動機 SAM)

來源:互聯網
上載者:User

http://codeforces.com/problemset/problem/235/C

陳立傑出的尾碼自動機,過的人挺少,不過還算是一道中規中矩的尾碼自動機吧。

題目大意:給一個字串S,再給一個字串T,設T的長度為len,問T的迴圈串在S中出現的次數,這裡迴圈串的定義是:對於一個長度為len的字串,我們把它首尾相接,然後從任意位置開始走len步所得到的串我們叫做T的迴圈串。如abaa的迴圈串有 abaa,baaa,aaab,aaba。(注意如果重複只算一次。比如aaa的迴圈串只有一個aaa)

 

思路:對於字串S,我們構造S的尾碼自動機,然後對於每一個字串T,我們設T'為T去掉最後一個字元所得到的字串,然後構造TT',在S的尾碼自動機上進行匹配,類似於LCS的做法,我們可以算出對於TT'的每一個位置,可以匹配的最大總長度,那麼當匹配長度大於等於len時(這裡的len為T的長度),設當前所在狀態為p,則我們可以根據par鏈找到匹配長度為len時所對應的狀態,設為q,則我們設狀態q所表示的子串出現的次數為q->num,則ans+=q->num,num的計算還是通過拓撲排序,自底向上求即可,注意這裡有可能有重複,所以我們還得在每一個狀態裡設一個標記flag,表示目前狀態是否被計算過,若已計算過則跳過即可。代碼如下:

#include <iostream>#include <string.h>#include <stdio.h>#define maxn 2000100#define Smaxn 26using namespace std;struct node{    node *par,*go[Smaxn];    int flag;    int num;    int val;}*root,*tail,que[maxn],*top[maxn];int tot;char str[maxn];void add(int c,int l){    node *p=tail,*np=&que[tot++];    np->val=l;    while(p&&p->go[c]==NULL)    p->go[c]=np,p=p->par;    if(p==NULL) np->par=root;    else    {        node *q=p->go[c];        if(p->val+1==q->val) np->par=q;        else        {            node *nq=&que[tot++];            *nq=*q;            nq->val=p->val+1;            np->par=q->par=nq;            while(p&&p->go[c]==q) p->go[c]=nq,p=p->par;        }    }    tail=np;}int c[maxn],len;void init(){    len=1;    tot=0;    memset(que,0,sizeof(que));    root=tail=&que[tot++];}char st[2000100];void solve(int n){    int i,j;    memset(c,0,sizeof(c));    for(i=0;i<tot;i++)    c[que[i].val]++;    for(i=1;i<len;i++)    c[i]+=c[i-1];    for(i=0;i<tot;i++)    top[--c[que[i].val]]=&que[i];    for(node *p=root;;p=p->go[str[p->val]-'a'])    {        p->num=1;        if (p->val==len-1)break;    }    for(i=tot-1;i>=0;i--)    {        node *p=top[i];        if(p->par)        {            p->par->num+=p->num;        }    }    int tmp=0;    node *p=root;    for(i=1;i<=n;i++)    {        long long ans=0;        scanf("%s",st);        int l=strlen(st);        memcpy(st+l,st,l);        int ll=2*l;        st[ll-1]='\0';        for(j=0;j<ll-1;j++)        {            int x=st[j]-'a';            if(p->go[x])            {                tmp++;                p=p->go[x];            }            else            {                while(p&&p->go[x]==NULL)                p=p->par;                if(p)                {                    tmp=p->val+1;                    p=p->go[x];                }                else                {                    tmp=0;                    p=root;                }            }            if(j>=l-1&&tmp>=l)            {                node *q=p;                while(1)                {                    if(l>=q->par->val+1&&l<=q->val)                    break;                    q=q->par;                }                if(q->flag!=i)                {                    ans+=q->num;                    q->flag=i;                }            }        }        printf("%I64d\n",ans);    }}int main(){    freopen("dd.txt","r",stdin);    scanf("%s",str);    init();    int i,l=strlen(str);    for(i=0;i<l;i++)    {        add(str[i]-'a',len++);    }    int n;    scanf("%d",&n);    solve(n);    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.