poj2774

来源:互联网 发布:蹭网软件下载 编辑:程序博客网 时间:2024/06/01 12:25

题目

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

题目大意

给你两个字符串,求他们的最长公共子串

解法

看到这道题之后想的大概是先把两个字符串接起来变成一个,然后做一下sa
那么答案肯定是一个第一个字符串开始的后缀和第二个字符串开始的后缀的lcp,也就是某一个sa[i],sa[i-1]分别在第一,第二个字符串里面的height[i]
但是考虑到lcp可能会存在越过两个字符串相接的那个位置的情况,那么我们取一个较小值就好了

贴代码

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<cstdio>#define fo(i,a,b) for(i=a;i<=b;i++)using namespace std;const int maxn=100005,ma1=200005;int sa[ma1],rank[ma1],height[ma1],x[ma1],y[ma1],buc[ma1],tmp[ma1];char s1[maxn],s2[maxn],s[ma1];int i,j,k,l,n,m,p,len1,len2,len,t1,t2,ans;void gesa(){    m=26;    fo(i,0,len-1) x[i]=s[i]-96;    fo(i,1,m) buc[i]=0;    fo(i,0,len-1) buc[x[i]]++;    fo(i,1,m) buc[i]+=buc[i-1];    fo(i,0,len-1) sa[buc[x[i]]--]=i;    for(k=1;k<len;k=k*2){        p=0;        fo(i,len-k,len-1) y[++p]=i;        fo(i,1,len) if (sa[i]>=k) y[++p]=sa[i]-k;        fo(i,1,m) buc[i]=0;        fo(i,0,len-1) buc[x[i]]++;        fo(i,1,m) buc[i]+=buc[i-1];        for(i=len;i>=1;i--) sa[buc[x[y[i]]]--]=y[i];        fo(i,0,len-1) y[i]=x[i];        x[sa[1]]=1;        fo(i,2,len){            if (y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k]) x[sa[i]]=x[sa[i-1]];            else x[sa[i]]=x[sa[i-1]]+1;        }        if (x[sa[len]]==len) break;        m=x[sa[len]];    }    fo(i,1,len) rank[sa[i]]=i;}void gehei(){    k=0;    fo(i,0,len-1){        if (rank[i]==1){            height[rank[i]]=0;            continue;        }        if (k) k--;        t1=i+k; t2=sa[rank[i]-1]+k;        while (t1<len && t2<len){            if (s[t1]==s[t2]) k++; else break;            t1++; t2++;        }        height[rank[i]]=k;    }}int main(){    //freopen("2774.in","r",stdin);    scanf("%s",&s1);    scanf("%s",&s2);    len1=strlen(s1); len2=strlen(s2);    len=len1+len2;    fo(i,0,len1-1) s[i]=s1[i];    fo(i,0,len2-1) s[i+len1]=s2[i];    gesa();    gehei();    ans=0;    fo(i,2,len){        t1=sa[i-1]; t2=sa[i];        if (t1>t2) swap(t1,t2);        if (t2>=len1 && t1<len1){            ans=max(ans,min(height[i],len1-t1));        }    }    printf("%d\n",ans);}
原创粉丝点击