现在的位置: 首页 > 综合 > 正文

poj 2774 Long Long Message(后缀数组)

2014年09月29日 ⁄ 综合 ⁄ 共 3810字 ⁄ 字号 评论关闭

Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 18965   Accepted: 7818
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

Source

POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."

题意:求2个字符串的最长相同子串

题解:把2个字符串合为一个,中间用一个没出现过的字符隔开,然后求height数组,遍历一次,若后缀数组相邻的2个后缀来自不同字符串则尝试更新答案

#include<stdio.h>
#include<string.h>
#define MAXN 200008
int rankx[MAXN],ranky[MAXN];
int sa[MAXN],height[MAXN];
int bucket[MAXN],r[MAXN];
char s1[MAXN],s2[MAXN];
int MIN(int x,int y){ return x<y?x:y; }
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void suffix_array(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=rankx,*y=ranky,*t;

    for(i=0;i<m;i++) bucket[i]=0;
    for(i=0;i<n;i++) bucket[x[i]=r[i]]++;
    for(i=1;i<m;i++) bucket[i]+=bucket[i-1];
    for(i=n-1;i>=0;i--) sa[--bucket[x[i]]]=i;
    for(p=j=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<m;i++) bucket[i]=0;
        for(i=0;i<n;i++) bucket[x[y[i]]]++;
        for(i=1;i<m;i++) bucket[i]+=bucket[i-1];
        for(i=n-1;i>=0;i--) sa[--bucket[x[y[i]]]]=y[i];
        t=x,x=y,y=t;
        for(p=i=1,x[sa[0]]=0;i<n;i++)
        x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
}
void cul_height(int *r,int *height,int *sa,int *x,int n)
{
    int i,j,k=0;
    for(i=0;i<=n;i++) x[sa[i]]=i;
    for(i=0;i<=n;height[x[i++]]=k)
    {
        if(x[i]==0) continue;
        for(k?k--:0,j=sa[x[i]-1];r[i+k]==r[j+k];k++);
    }
}
int check(int x,int len)
{
    if(sa[x]<len&&sa[x-1]>len) return 1;
    if(sa[x]>len&&sa[x-1]<len) return 1;
    return 0;
}
int main()
{
    int len1,len2,i,j,res,n;

    while(scanf("%s%s",s1,s2)>0)
    {
        len1=strlen(s1);
        len2=strlen(s2);
        
        for(i=0;i<len1;i++) r[i]=s1[i]-'a'+2;
        r[i]=1,r[i+len2+1]=j=0;
        for(i++;j<len2;j++,i++) r[i]=s2[j]-'a'+2;
        suffix_array(r,sa,len1+1+len2+1,30);
        cul_height(r,height,sa,rankx,len1+1+len2);
        
        for(n=i,res=0,i=2;i<=n;i++)
        {
            if(height[i]>res&&check(i,len1))
                res=height[i];
        }
        printf("%d\n",res);
    }

    return 0;
}

抱歉!评论已关闭.