poj3080 (二分+后缀数组)简单题

求最长的公共字串。
#include<stdio.h>
#include<string.h>
#define max 610
int next[max],sa[max];
int wa[max],wb[max],wv[max],ws[max];
int rank[max],height[max];
int vis[max];
char tem[max];
int str[max],lcp[max],n;
char ans[max];
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws[i]=0;
    for(i=0; i<n; i++) ws[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--) sa[--ws[x[i]]]=i;
    for(j=1,p=1; p<n; j*=2,m=p)
    {
        for(p=0,i=n-j; i<n; i++) y[p++]=i;
        for(i=0; i<n; i++) if(sa[i]>=j)y[p++]=sa[i]-j;
        for(i=0; i<n; i++)wv[i]=x[y[i]];
        for(i=0; i<m; i++)ws[i]=0;
        for(i=0; i<n; i++)ws[wv[i]]++;
        for(i=1; i<m; i++) ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--) sa[--ws[wv[i]]]=y[i];
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    return ;
}
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
    return ;
}
int check(int mid,int len)
{
    int count=0;
    memset(vis,0,sizeof(vis));
    for(int i=2; i<=len; i++)
    {
        if(height[i]<mid)
        {
            memset(vis,0,sizeof(vis));
            count=0;
            continue;
        }
        if(!vis[lcp[sa[i]]])  vis[lcp[sa[i]]]=1,count++;
        if(!vis[lcp[sa[i-1]]])  vis[lcp[sa[i-1]]]=1,count++;
        if(count==n) return 1;
    }
    return 0;
}
void output(int mid,int len)
{
    int count=0;
    memset(vis,0,sizeof(vis));
    for(int i=2; i<=len; i++)
    {
        if(height[i]<mid)
        {
            memset(vis,0,sizeof(vis));
            count=0;
            continue;
        }
        if(!vis[lcp[sa[i]]])  vis[lcp[sa[i]]]=1,count++;
        if(!vis[lcp[sa[i-1]]])  vis[lcp[sa[i-1]]]=1,count++;
        if(count==n)
        {
            for(int j=0; j<mid; j++)
                ans[j]=str[sa[i]+j]+'A'-1;
            ans[mid]='\0';
            printf("%s",ans);
            break;
        }
    }
    return ;
}
int main()
{
    int len;
    int min;
    int sumLen;
    int s;
    scanf("%d",&s);
    while(s--)
    {
        scanf("%d",&n);
        {
            memset(str,'\0',sizeof(str));
            min=max;
            sumLen=0;
            int m=30;
            for(int i=0; i<n; i++)
            {
                scanf("%s",tem);
                len=strlen(tem);
                if(min>len) min=len;
                for(int j=0; j<len; j++)
                {
                    str[sumLen]=tem[j]-'A'+1;
                    lcp[sumLen++]=i;
                }
                str[sumLen]=m;
                lcp[sumLen++]=m++;
            }
            str[sumLen]=0;
            da(str,sa,sumLen+1,m);
            calheight(str,sa,sumLen);
            int left=0,right=min,mid;
            int flag=0;
            while(right>=left)//二分
            {
                mid=(right+left)/2;
                if(check(mid,sumLen))
                {
                    left=mid+1;
                    flag=mid;
                }
                else
                {
                    right=mid-1;
                }
            }
            if(flag&&mid>=3)
            {
                output(flag,sumLen);
            }
            else
                printf("no significant commonalities");
            printf("\n");
        }
    }
    return 0;
}

你可能感兴趣的:(poj3080 (二分+后缀数组)简单题)