POJ2774 Long Long Message

Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 29639   Accepted: 12034
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."

 

求两个串的最长公共子串

后缀数组模板题

35行的swap(wa,wb)在POJ上选C++编译会RE,选G++就没事

如果非要用C++,就得改成指针形式(我可能知道别人的模板为什么流行用指针了)

 

按照后缀数组的定义,height数组记录的是rank为i的串和rank为i-1的串的最长公共前缀。

现在把两个字符串合在一起,求一遍后缀数组,如果rank为i的串和rank为i-1的串一个是从前一个串开始的,另一个是从后一个串开始的,那么它们的height值就是两串在对应位置起始的最长公共前缀。

在两串之间加一个两串中都没有的特殊符号,求height时就不会出现匹配bug了

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=200010;
 9 int wa[mxn],wb[mxn],wv[mxn];
10 int sa[mxn],rank[mxn];
11 int ht[mxn];
12 int cnt[mxn];
13 int s[mxn];
14 char c1[mxn],c2[mxn];
15 inline bool cmp(int *s,int a,int b,int k){
16     return (s[a]==s[b] && s[a+k]==s[b+k]);
17 }
18 void getsa(int n,int m){
19     int i,j;
20     memset(cnt,0,sizeof cnt);
21     for(i=0;i<n;i++)wa[i]=s[i];    
22     for(i=0;i<n;i++)cnt[wa[i]]++;
23     for(i=1;i<m;i++)cnt[i]+=cnt[i-1];
24     for(i=n-1;i>=0;i--)sa[--cnt[wa[i]]]=i;
25     //计数排序 
26     int p=1;
27     for(j=1;p<n;j<<=1,m=p){
28         for(p=0,i=n-j;i<n;i++)wb[p++]=i;
29         for(i=0;i<n;i++)if(sa[i]>=j)wb[p++]=sa[i]-j;
30         for(i=0;i<n;i++)wv[i]=wa[wb[i]];
31         for(i=0;i<m;i++)cnt[i]=0;
32         for(i=0;i<n;i++)cnt[wv[i]]++;
33         for(i=1;i<m;i++)cnt[i]+=cnt[i-1];
34         for(i=n-1;i>=0;i--)sa[--cnt[wv[i]]]=wb[i];
35         swap(wa,wb);
36         wa[sa[0]]=0;
37         for(p=1,i=1;i<n;i++)
38             wa[sa[i]]=cmp(wb,sa[i-1],sa[i],j)?p-1:p++;
39     }
40     return;
41 }
42 void cal_height(int n){
43     int i,j,k=0;
44     for(i=1;i<=n;i++)rank[sa[i]]=i;
45     for(i=0;i<n;i++){
46         if(k)k--;
47         j=sa[rank[i]-1];
48         while(s[i+k]==s[j+k])k++;
49         ht[rank[i]]=k;
50     }
51     return;
52 }
53 int main(){
54     int i,j,n;
55     while(scanf("%s",c1)!=EOF){
56         n=0;
57         scanf("%s",c2);
58         int len=strlen(c1);
59         for(i=0;i<len;i++)s[n++]=c1[i]-'a'+1;
60         s[n++]=28;//隔断符号 
61         len=strlen(c2);
62         for(i=0;i<len;i++)s[n++]=c2[i]-'a'+1;
63         s[n]=0;
64         getsa(n+1,30);
65         cal_height(n);
66         int mx=0;
67         len=strlen(c1);
68         for(i=2;i<n;i++){
69             if(ht[i]>mx){
70                 if(sa[i-1]>=0 && sa[i-1]<len && sa[i]>=len)
71                     mx=ht[i];
72                 if(sa[i]>=0 && sa[i]<len && sa[i-1]>=len)
73                     mx=ht[i];
74             }
75         }
76         printf("%d\n",mx);
77     }
78     return 0;
79 }

 

posted @ 2017-02-14 23:47  SilverNebula  阅读(166)  评论(0编辑  收藏  举报
AmazingCounters.com