HDU 3518 boring counting (suffix automatic machine Sam)

Source: Internet
Author: User

Evaluate the number of different strings that repeat more than two times but do not overlap. For example, in AAAA, A and AA meet the requirements, while AAA and AAAA do not.

Train of Thought: It seems that the mark is a suffix array, but I used a suffix automatic machine. We only need to set L in every state in the suffix automatic machine, r indicates the leftmost and rightmost positions of the substring set in this state, respectively. Num indicates the total number of substrings in this state.

Because in the suffix automatic machine, each State represents a type of substrings (set a state to P) whose length range is [p-> par-> Val + 1, p-> Val], (set to left or right), if p-> r-p-> L> = p-> Val. it indicates that every string in this State meets the requirements. Therefore, right-left + 1 should be added to the answer. If P-> r-p-> L is between left and right, it indicates that only a part of the sub-string (that is, the sub-string with a length not greater than P-> r-p-> L) meets the requirements, the answer should be p-> r-p-> L-left + 1. As for the update of left, right, and num, by the nature of Sam, we can sort the Sam topology first, then update from bottom to top. The Code is as follows:

#include <iostream>#include <string.h>#include <stdio.h>#define maxn 2010#define Smaxn 26#define mod 2012#define inf 21000000using namespace std;struct node{    node *par,*go[Smaxn];    int left,right;    int num;    int po;    int val;}*root,*tail,que[maxn],*top[maxn];int tot;char str[maxn>>1];void add(int c,int l,int po){    node *p=tail,*np=&que[tot++];    np->val=l;    np->po=po;    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(){    memset(que,0,sizeof(que));    tot=0;    len=1;    root=tail=&que[tot++];}void solve(){    memset(c,0,sizeof(c));    int i;    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+1]-'a'])    {        p->num=1;        p->left=p->right=p->po;        if (p->val==len-1)break;    }    for(i=tot-1;i>=0;i--)    {        node *p=top[i];        if(p->left==0&&p->right==0)        {            p->left=p->right=p->po;        }        if(p->par)        {            node *q=p->par;            q->num+=p->num;            if(q->left==0||q->left>p->left)            q->left=p->left;            if(q->right==0||q->right<p->right)            q->right=p->right;        }    }    int ans=0;    for(i=1;i<tot;i++)    {        if(que[i].num>1)        {            int ma=que[i].val,mi=que[i].par->val+1,le=que[i].right-que[i].left;            if(le>=ma)            ans+=(ma-mi+1);            else if(le<ma&&le>=mi)            {                ans+=(le-mi+1);            }        }    }    printf("%d\n",ans);}int main(){    while(1)    {        scanf("%s",str+1);        int i,l=strlen(str+1);        if(str[1]=='#')        break;        init();        for(i=1;i<=l;i++)        {            add(str[i]-'a',len++,i);        }        solve();    }    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.