poj 2774 求兩字串的最長公共子串 字尾陣列

life4711發表於2014-08-04

http://poj.org/problem?id=2774

Language:
Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 20556   Accepted: 8475
Case Time Limit: 1000MS

Description

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. 

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 

1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. 

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. 

Background: 
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. 

Why ask you to write a program? There are four resions: 
1. The little cat is so busy these days with physics lessons; 
2. The little cat wants to keep what he said to his mother seceret; 
3. POJ is such a great Online Judge; 
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output

27

Sour

題目大意:求出給出字串的最長公共子串

解題思路:

                 熟悉字尾陣列的人都知道要求兩個字串的最長公共子串就是求任意兩字尾的最長公共字首。因此我們可以利用字尾陣列的height陣列。先用個分隔符將兩個字串連線起來,再用字尾陣列求出height陣列的值,找出一個height值最大並且i與i-1的sa值分別在兩串字元中就好.....

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=2111111;
/******************************************************************
**  字尾陣列 Suffix Array
**  INIT:solver.call_fun(char* s);
**  CALL: solver.lcp(int i,int j); //字尾i與字尾j的最長公共字首
**  SP_USE: solver.LCS(char *s1,char* s2); //最長公共字串
******************************************************************/
struct SuffixArray
{
    int r[maxn];
    int sa[maxn],rank[maxn],height[maxn];
    int t[maxn],t2[maxn],c[maxn],n;
    int m;//模板長度
    void init(char* s)
    {
        n=strlen(s);
        for (int i=0; i<n; i++) r[i]=int(s[i]);
        m=300;
    }
    int cmp(int *r,int a,int b,int l)
    {
        return r[a]==r[b]&&r[a+l]==r[b+l];
    }
    /**
    字元要先轉化為正整數
    待排序的字串放在r[]陣列中,從r[0]到r[n-1],長度為n,且最大值小於m。
    所有的r[i]都大於0,r[n]無意義演算法中置0
    函式結束後,結果放在sa[]陣列中(名次從1..n),從sa[1]到sa[n]。s[0]無意義
    **/
    void build_sa()
    {
        int i,k,p,*x=t,*y=t2;
        r[n++]=0;
        for (i=0; i<m; i++) c[i]=0;
        for (i=0; i<n; i++) c[x[i]=r[i]]++;
        for (i=1; i<m; i++) c[i]+=c[i-1];
        for (i=n-1; i>=0; i--) sa[--c[x[i]]]=i;
        for (k=1,p=1; k<n; k*=2,m=p)
        {
            for (p=0,i=n-k; i<n; i++) y[p++]=i;
            for (i=0; i<n; i++) if (sa[i]>=k) y[p++]=sa[i]-k;
            for (i=0; i<m; i++) c[i]=0;
            for (i=0; i<n; i++) c[x[y[i]]]++;
            for (i=1; i<m; i++) c[i]+=c[i-1];
            for (i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;
            x[sa[0]]=0;
            for (i=1; i<n; i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],k)?p-1:p++;
        }
        n--;
    }
    /**
    height[2..n]:height[i]儲存的是lcp(sa[i],sa[i-1])
    rank[0..n-1]:rank[i]儲存的是原串中suffix[i]的名次
    **/
    void getHeight()
    {
        int i,j,k=0;
        for (i=1; i<=n; i++) rank[sa[i]]=i;
        for (i=0; i<n; i++)
        {
            if (k) k--;
            j=sa[rank[i]-1];
            while (r[i+k]==r[j+k]) k++;
            height[rank[i]]=k;
        }
    }
    int d[maxn][20];
    //元素從1編號到n
    void RMQ_init(int A[],int n)
    {
        for (int i=1; i<=n; i++) d[i][0]=A[i];
        for (int j=1; (1<<j)<=n; j++)
            for (int i=1; i+j-1<=n; i++)
                d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
    }
    int RMQ(int L,int R)
    {
        int k=0;
        L=rank[L];
        R=rank[R];
        if(L>R) swap(L,R);
        L++;
        while ((1<<(k+1))<=R-L+1) k++;
        return min(d[L][k],d[R-(1<<k)+1][k]);
    }
    void LCP_init()
    {
        RMQ_init(height,n);
    }
    int lcp(int i,int j)
    {
        if (rank[i]>rank[j]) swap(i,j);
        return RMQ(rank[i]+1,rank[j]);
    }
    void call_fun(char* s)
    {
        init(s);//初始化字尾陣列
        build_sa();//構造字尾陣列sa
        getHeight();//計算height與rank
        LCP_init();//初始化RMQ
    }
    int solve(int len)
    {
        int maxx=0,pos=0;
        for(int i=2; i<n; i++)
        {
            if(height[i]>maxx)
            {
                if(0<=sa[i-1]&&sa[i-1]<len&&len<sa[i])
                    maxx=height[i];
                if(0<=sa[i]&&sa[i]<len&&len<sa[i-1])
                    maxx=height[i];
            }
        }
        return maxx;
    }
} solver;
int main()
{
    char str[maxn],str1[maxn],s[maxn];
    while(~scanf("%s",str))
    {
        scanf("%s",str1);
        int n=0,len=strlen(str);
        for(int i=0; i<len; i++)
            s[n++]=str[i];
        s[n++]='$';
        int len1=strlen(str1);
        for(int i=0; i<len1; i++)
            s[n++]=str1[i];
        s[n]=0;
        solver.call_fun(s);
        int maxx=solver.solve(len);
        printf("%d\n",maxx);
    }
    return 0;
}
/**

dkghaabcdefghijklmnopqrstuvwxy
kghsghasabcdefghijklmnopqrstuvwxyzg

**/



相關文章