Poj 1226 substrings (suffix array)

Source: Internet
Author: User

Question:

This is a substring that appears in all M strings. The substring can also appear in the reverse sequence of the substring.


Train of Thought Analysis:

Actually Wa has five "A" data records.

The lower bound cannot be 0 in binary mode ..

In general, the idea is to process all the original and reverse strings, put them in STR, and then add one between each string that has not appeared.

Note that the input is not just a letter.

Run the suffix array again.

Then, use the mark to count.


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#define maxn 100005using namespace std;int str[maxn];int sa[maxn],t1[maxn],t2[maxn],c[maxn],n;void suffix(int m){    int *x=t1,*y=t2;    for(int i=0; i<m; i++)c[i]=0;    for(int i=0; i<n; i++)c[x[i]=str[i]]++;    for(int i=1; i<m; i++)c[i]+=c[i-1];    for(int i=n-1; i>=0; i--)sa[--c[x[i]]]=i;    for(int k=1; k<=n; k<<=1)    {        int p=0;        for(int i=n-k; i<n; i++)y[p++]=i;        for(int i=0; i<n; i++)if(sa[i]>=k)y[p++]=sa[i]-k;        for(int i=0; i<m; i++)c[i]=0;        for(int i=0; i<n; i++)c[x[y[i]]]++;        for(int i=0; i<m; i++)c[i]+=c[i-1];        for(int i=n-1; i>=0; i--)sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;        x[sa[0]]=0;        for(int i=1; i<n; i++)            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;        if(p>=n)break;        m=p;    }}int rank[maxn],height[maxn];void getheight(){    int k=0;    for(int i=0; i<n; i++)rank[sa[i]]=i;    for(int i=0; i<n; i++)    {        if(k)k--;        if(!rank[i])continue;        int j=sa[rank[i]-1];        while(str[i+k]==str[j+k])k++;        height[rank[i]]=k;    }}char tmp[105];bool vis[105];int dex[maxn];bool check(int len,int m){    int cnt=m;    memset(vis,false,sizeof vis);    for(int i=1;i<n;i++)    {        if(height[i]<len)        {            if(!cnt)return true;            cnt=m;            memset(vis,false,sizeof vis);        }        else        {            if(!vis[dex[sa[i-1]]])            {                cnt--;                vis[dex[sa[i-1]]]=true;            }            if(!vis[dex[sa[i]]])            {                cnt--;                vis[dex[sa[i]]]=true;            }        }    }    return false;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        n=0;        int m;        scanf("%d",&m);        for(int i=1;i<=m;i++)        {            scanf("%s",tmp);            int len=strlen(tmp);            for(int j=0;j<len;j++)            {                dex[n]=i;                str[n++]=tmp[j];            }            dex[n]=i;            str[n++]=128+i*2-1;            reverse(tmp,tmp+len);            for(int j=0;j<len;j++)            {                dex[n]=i;                str[n++]=tmp[j];            }            dex[n]=i;            str[n++]=128+i*2;        }        str[n-1]=0;        suffix(400);        getheight();        int l=1,r=100,ans=0;        while(l<=r)        {            int mid=(l+r)>>1;            if(check(mid,m))            ans=mid,l=mid+1;            else r=mid-1;        }        printf("%d\n",ans);    }    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.