POJ - 3294 - Life Forms(後綴數組,二分+分組技巧)

POJ - 3294 - Life Forms(後綴數組,二分+分組技巧)

題目鏈接:傳送門

思路:

  • part1

  • 對於n個字符串,我們只需要找至少在n/2+1個字符串出現過的最長子串即可。

  • part2

  • 如果k=1,我們只需輸出最長的字符串即可(這裏n肯定等於1,即本身)

  • 否則我們首先將這n個字符串用不相同特殊字符連接成一個字符串S,並記錄每個下標位置屬於哪個字符串。

  • 二分所求子串的長度w,然後在height[]height[]數組上進行分組,即height[]<wheight[]<w的位置分割開,這樣我們就得到了許多組,我們只需判斷是否存在一組內有k個字符串的後綴即可。(因爲後綴的最長相等前綴>=w>=w不會跨越組,所以如果存在,答案一定在某個組內,否則也不存在,某個組存在最少k個字符串的後綴串,所以算法正確)

  • part3

  • 現在我們找到了這個最長子串長度w,我們只需模仿上面分組,如果組內有解就輸出這個組的最長相同前綴即可。

代碼:

#include<string>
#include<algorithm>
#include<stdio.h>
#include<iostream>
#include<string.h>
#define mset(a,b)   memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
const int N=2e5+10;
int t1[N],t2[N],c1[N];//輔助數組
void SA(int *s,int n,int sa[],int rnk[],int height[])//基數排序的版本中
{
    int m=300;
    int *sb=t1,*r=t2,*c=c1;
    for(int i=0; i<=m; ++i) c[i]=0;
    for(int i=1; i<=n; ++i) c[r[i]=s[i]]++;
    for(int i=1; i<=m; ++i) c[i]+=c[i-1];
    for(int i=n; i>=1; --i) sa[c[s[i]]--]=i;

    for(int k=1,p ; k < n; k<<=1 )
    {
        p=0;
        for(int i=n-k+1; i<=n; ++i) sb[++p]=i;
        for(int i=1; i<=n; ++i) if(sa[i]>k) sb[++p]=sa[i]-k;
        for(int i=0; i<=m; ++i) c[i]=0;
        for(int i=1; i<=n; ++i) c[r[i]]++;
        for(int i=1; i<=m; ++i) c[i]+=c[i-1];
        for(int i=n; i>=1; --i) sa[ c[r[sb[i]]]-- ]=sb[i];
        std::swap(r,sb);
        r[sa[1]]=p=1;
        for(int i=2,a,b; i<=n; ++i)
        {
            a=sa[i],b=sa[i-1];
            if(sb[a]==sb[b]&&(a+k<=n && b+k<=n&&sb[a+k]==sb[b+k]) ) r[a]=p;
            else r[a]=++p;
        }
        if(p>=n) break;//可以提前退出
        m=p;
    }
    int k=0;
    for(int i=1; i<=n; ++i) rnk[sa[i]]=i;
    height[1]=0;
    for(int i=1; i<=n; ++i)
    {
        if(k) k--;
        int j=sa[rnk[i]-1];
        if(j==0) continue;
        while(s[j+k]==s[i+k]) ++k;
        height[rnk[i]]=k;
    }
}
int rnk[N],sa[N],height[N]; //height[1]的值爲0
int w[N];
string s[105];
int n,book[105],belong[N];
bool judge(int v,int ls,int k)//出現的長度爲v,至少出現在k個字符串中
{
    mset(book,0);
    int c=0;
    if(belong[sa[1]]!=-1)   book[belong[sa[1]]]=1,c=1;
    int l=1;
    for(int i=2; i<=ls; ++i)
    {
        if(height[i]>=v)//i not is se
        {
            if(book[belong[sa[i]]]==0)//
            {
                book[belong[sa[i]]]=1;
                c++;
            }
        }
        else
        {
            l=i;
            if(c>=k) return true;
            mset(book,0);
            c=0;
            if(belong[sa[i]]!=-1)   book[belong[sa[i]]]=1,c=1;
        }
    }
    if(c>=k) return true;
    else return false;
}
void ptans(int v,int ls,int k)
{
    mset(book,0);
    int c=0;
    if(belong[sa[1]]!=-1)   book[belong[sa[1]]]=1,c=1;
    for(int i=2; i<=ls; ++i)
    {
        if(height[i]>=v)//i not is se
        {
            if(book[belong[sa[i]]]==0)//
            {
                book[belong[sa[i]]]=1;
                c++;
            }
        }
        else
        {
            if(c>=k)  //sa[i-1],len:v
            {
                for(int j=0; j<v; ++j)
                    printf("%c",char(w[sa[i-1]+j]-100));
                puts("");
            }
            mset(book,0);
            c=0;
            if(belong[sa[i]]!=-1)   book[belong[sa[i]]]=1,c=1;
        }
    }
    if(c>=k)  //sa[i-1],len:v
    {
        for(int j=0; j<v; ++j)
            printf("%c",char(w[sa[ls]+j]-100));
        puts("");
    }
}
int main()
{
    bool have=false;
    while(scanf("%d",&n),n)
    {
        if(have) puts("");
        have=true;
        int ls=0;
        for(int i=1; i<=n; ++i)
        {
            cin>>s[i];
            for(int j=0;j<s[i].size();++j)
            {
                w[++ls]=int(s[i][j])+100;
                belong[ls]=i;
            }
            w[++ls]=int(i);
            belong[ls]=-1;
        }
        if(n==1)
            cout<<s[1]<<endl;
        w[ls+1]=0;
        SA(w,ls,sa,rnk,height);
        int l=1,r=ls,ans=-1,k=(n+2)/2;
        while(l<=r)
        {
            int m=l+r>>1;
            if(judge(m,ls,k))
            {
                ans=m;
                l=m+1;
            }
            else r=m-1;
        }
        if(ans==-1)
            printf("?\n");
        else
            ptans(ans,ls,k);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章