Relevant Phrases of Annihilation SPOJ - PHRASES 後綴數組

PHRASES - Relevant Phrases of Annihilation

no tags 

You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

Input

The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

Output

For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

Example

Input:
1
4
abbabba
dabddkababa
bacaba
baba

Output:
2

(in the example above, the longest substring which fulfills the requirements is 'ba')

題意:求至少出現兩次的最長重複子串
二分答案

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include <iostream>
#include <string>
#include <set>
#include <map>
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
const int MAXN = 500000+10;
const int N = MAXN;
const int INF=1e9+7;
int wa[MAXN],wb[MAXN],wv[MAXN],Ws[MAXN];
int sa[MAXN],height[MAXN],RANK[MAXN];
int len[MAXN];
int c0(int *r,int a,int b)
{
    return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b)
{
    if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
    else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void sort(int *r,int *a,int *b,int n,int m)
{
    int i;
    for(i=0;i<n;i++) wv[i]=r[a[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--) b[--Ws[wv[i]]]=a[i];
    return;
}
void dc3(int *r,int *sa,int n,int m) //涵義與DA 相同
{
    int i,j,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    int *rn=r+n;
    r[n]=r[n+1]=0;
    for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;
    sort(r+2,wa,wb,tbc,m);
    sort(r+1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0;i<tbc;i++) san[rn[i]]=i;
    for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    sort(r,wb,wa,ta,m);
    for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0;i<ta && j<tbc;p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(;i<ta;p++) sa[p]=wa[i++];
    for(;j<tbc;p++) sa[p]=wb[j++];
    return;
}
void calheight(int *r,int *sa,int n)
{   // 此處N爲實際長度
    int i,j,k=0;
    // height[]的合法範圍爲 1-N, 其中0是結尾加入的字符
    for(i=1;i<=n;i++)
        RANK[sa[i]]=i;
    // 根據SA求RANK
    for(i=0;i<n; height[RANK[i++]] = k )
        // 定義:h[i] = height[ RANK[i] ]
        for(k?k--:0,j=sa[RANK[i]-1];
            r[i+k]==r[j+k]; k++);
    //根據 h[i] >= h[i-1]-1 來優化計算height過程
}

int dp[MAXN][20];
void Rmq_Init(int n){
    int m=floor(log(n+0.0)/log(2.0));
    for(int i=1;i<=n;i++) dp[i][0]=height[i];
    for(int i=1;i<=m;i++){
        for(int j=n;j;j--){
            dp[j][i]=dp[j][i-1];
            if(j+(1<<(i-1))<=n)
                dp[j][i]=min(dp[j][i],dp[j+(1<<(i-1))][i-1]);
        }
    }
}
int Rmq_Query(int l,int r){
    int a=RANK[l],b=RANK[r];
    if(a>b) swap(a,b);
    a++;
    int m=floor(log(b-a+1.0)/log(2.0));
    return min(dp[a][m],dp[b-(1<<m)+1][m]);
}

string s[15];
int a[MAXN];
int n;
int tot=0;
int l[15];
int vis[2][15];
bool C(int k){
    int num=0;
    for(int i=n;i<=tot;i++){
        if(height[i]<k){
            memset(vis,-1,sizeof vis);
            num=0;
        }
        int x=(int)(lower_bound(l, l+n, sa[i])-l);
        if(vis[0][x]==-1){
            vis[0][x]=vis[1][x]=sa[i];
        }else if(vis[0][x]!=-2){
            if(max(sa[i],vis[0][x])-min(sa[i],vis[0][x])>=k||max(sa[i],vis[1][x])-min(sa[i],vis[1][x])>=k){
                num++;
                vis[0][x]=-2;
            }else{
                vis[0][x]=min(vis[0][x],sa[i]);
                vis[1][x]=max(vis[1][x],sa[i]);
            }
        }
        if(num==n)
            return true;
    }
    return false;
}
void solve(){
    int L=0,R=10010;
    while(R>L+1){
        int m=(L+R)>>1;
        if(C(m))
            L=m;
        else
            R=m;
    }
    printf("%d\n",L);
}
int main(){
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        tot=0;
        for(int i=0;i<n;i++){
            cin>>s[i];
            int m=(int)s[i].length();
            for(int j=0;j<m;j++)
                a[tot++]=(int)s[i][j];
            a[tot]=1;
            l[i]=tot;
            tot++;
        }
        tot--;
        a[tot]=0;
        dc3(a, sa, tot+1, 200);
        calheight(a, sa, tot);
        Rmq_Init(tot);
        solve();
    }
    
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章