poj2774 Long Long Message(后缀数组)

来源:互联网 发布:天庭淘宝店无常 编辑:程序博客网 时间:2024/06/06 13:56
Long Long Message

Time Limit: 4000MS

 

Memory Limit: 131072K

Total Submissions: 24477

 

Accepted: 10030

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 mobileservice center, to check how much money he has spent on SMS. Yesterday, thecomputer of service center was broken, and printed two very long messages. Thebrilliant little cat soon found out:

1. All characters in messages are lowercase Latin letters, without punctuationsand spaces.
2. All SMS has been appended to each other – (i+1)-th SMS comes directly afterthe 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 redundancycharacters appear leftwards and rightwards due to the broken computer.
E.g: if his SMS is “motheriloveyou”, either long message printed by thatmachine, 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 textremains the same in two printed messages, the redundancy characters on bothsides would be possibly different.

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

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

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 hismother to see the doctor :(

Input

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

Output

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

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit

yeaphowmuchiloveyoumydearmother

Sample Output

27

Source

POJMonthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great belovedmother."

 

【思路】

       A& B的最长公共子序列。

       拼接+height数组。将AB拼接成一个形如A$B的串,枚举height数组并判断sa[i]是否与sa[i-1]分别属于两个不同的字符串,如果是则比较得ans。

   时间复杂度为O(nlogn)。

       Ps:注意不能直接取height的最大值,因为取到的两个后缀的lcp可能同处于一个字符串。

【代码】

 

 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #define FOR(a,b,c) for(int a=(b);a<=(c);a++) 5 using namespace std; 6  7 const int maxn = 300000+10;  8 const int maxm = 150; 9 10 int s[maxn];11 int sa[maxn],c[maxm],t[maxn],t2[maxn];12 13 void build_sa(int m,int n) {14     int i,*x=t,*y=t2;15     for(i=0;i<m;i++) c[i]=0;16     for(i=0;i<n;i++) c[x[i]=s[i]]++;17     for(i=1;i<m;i++) c[i]+=c[i-1];18     for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;19     20     for(int k=1;k<=n;k<<=1) {21         int p=0;22         for(i=n-k;i<n;i++) y[p++]=i;23         for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;24         25         for(i=0;i<m;i++) c[i]=0;26         for(i=0;i<n;i++) c[x[y[i]]]++;27         for(i=0;i<m;i++) c[i]+=c[i-1];28         for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];29         30         swap(x,y);31         p=1; x[sa[0]]=0;32         for(i=1;i<n;i++) 33             x[sa[i]]=y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;34         if(p>=n) break;35         m=p;36     }37 }38 int rank[maxn],height[maxn];39 void getHeight(int n) {40     int i,j,k=0;41     for(i=0;i<=n;i++) rank[sa[i]]=i;42     for(i=0;i<n;i++) {43         if(k) k--;44         j=sa[rank[i]-1];45         while(s[j+k]==s[i+k]) k++;46         height[rank[i]]=k;47     }48 }49 50 char a[maxn],b[maxn];51 int n;52 53 int main() {54     scanf("%s%s",a,b);55     int lena=strlen(a),lenb=strlen(b);56     n=lena+lenb+1;57     for(int i=0;i<lena;i++) s[i]=a[i]; s[lena]=1;58     for(int i=0;i<lenb;i++) s[lena+i+1]=b[i];59     s[n]=0;60     61     build_sa('z'+1,n+1);62     getHeight(n);63     64     int ans=0;65     for(int i=2;i<=n;i++) {66         int k=0;67         k+=sa[i]<lena?1:2;68         k+=sa[i-1]<lena?1:2;69         if(k==3) ans=max(ans,height[i]);70     }71     printf("%d\n",ans);72     return 0;73 }

 

0 0
原创粉丝点击