【POJ2774】Long Long Message (后缀数组)

Long Long Message

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

【题意】

  给你两串字符,要你找出在这两串字符中都出现过的最长子串。

 

【分析】

  把两个串拼起来,中间插入特殊字符。

  for一遍,用相邻的A、B串的LCP更新到ans中即可。

 

代码如下:

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<queue>
  7 using namespace std;
  8 #define INF 0xfffffff
  9 #define Maxl 200010
 10 #define Mod 256
 11 
 12 int k,la;
 13 char a[Maxl],b[Maxl];
 14 int c[Maxl];
 15 int cl;
 16 
 17 int sa[Maxl],rk[Maxl],Rs[Maxl],wr[Maxl],y[Maxl];
 18 //sa -> 排名第几的是谁
 19 //rk -> i的排名
 20 //Rs数值小于等于i的有多少个
 21 //y -> 第二关键字排名第几的是谁(类似sa)
 22 int height[Maxl];
 23 
 24 int mymin(int x,int y) {return x<y?x:y;}
 25 int mymax(int x,int y) {return x>y?x:y;}
 26 
 27 void get_sa(int m)
 28 {
 29    memcpy(rk,c,sizeof(rk));
 30     for(int i=0;i<=m;i++) Rs[i]=0;
 31     for(int i=1;i<=cl;i++) Rs[rk[i]]++;
 32     for(int i=1;i<=m;i++) Rs[i]+=Rs[i-1];
 33     for(int i=cl;i>=1;i--) sa[Rs[rk[i]]--]=i;
 34     
 35     int ln=1,p=0;
 36     while(p<cl)
 37     {
 38        int k=0;
 39         for(int i=cl-ln+1;i<=cl;i++) y[++k]=i;
 40         for(int i=1;i<=cl;i++) if(sa[i]>ln) y[++k]=sa[i]-ln;
 41         for(int i=1;i<=cl;i++) wr[i]=rk[y[i]];
 42         
 43         for(int i=0;i<=m;i++) Rs[i]=0;
 44         for(int i=1;i<=cl;i++) Rs[wr[i]]++;
 45         for(int i=1;i<=m;i++) Rs[i]+=Rs[i-1];
 46         for(int i=cl;i>=1;i--) sa[Rs[wr[i]]--]=y[i];
 47         
 48         for(int i=1;i<=cl;i++) wr[i]=rk[i];
 49         for(int i=cl+1;i<=cl+ln;i++) wr[i]=0;
 50         p=1;rk[sa[1]]=1;
 51         for(int i=2;i<=cl;i++)
 52         {
 53             if(wr[sa[i]]!=wr[sa[i-1]]||wr[sa[i]+ln]!=wr[sa[i-1]+ln]) p++;
 54             rk[sa[i]]=p;
 55         }
 56         m=p,ln*=2;
 57     }
 58     sa[0]=rk[0]=0;
 59 }
 60 
 61 void get_he()
 62 {
 63     int kk=0;
 64     for(int i=1;i<=cl;i++)
 65     {
 66         int j=sa[rk[i]-1];
 67         if(kk) kk--;
 68         while(c[i+kk]==c[j+kk]&&i+kk<=cl&&j+kk<=cl) kk++;
 69         height[rk[i]]=kk;
 70     }
 71 }
 72 
 73 void ffind()
 74 {
 75     int na=INF,nb=INF,ans=0;
 76     for(int i=1;i<cl;i++)
 77     {
 78         if(sa[i]<=la) //a串
 79         {
 80             if(nb<=cl) ans=mymax(nb,ans),
 81             nb=mymin(nb,height[i+1]);
 82             na=height[i+1];
 83         }
 84         else //b串
 85         {
 86             if(na<=cl) ans=mymax(na,ans),
 87             na=mymin(na,height[i+1]);
 88             nb=height[i+1];
 89         }
 90     }
 91     printf("%d\n",ans);
 92 }
 93 
 94 void init()
 95 {
 96     scanf("%s%s",a,b);
 97     int l=strlen(a);la=l;
 98     for(int i=0;i<l;i++) c[++cl]=a[i]-'a'+1;
 99     l=strlen(b);
100     c[++cl]=30;
101     for(int i=0;i<l;i++) c[++cl]=b[i]-'a'+1;
102 }
103 
104 int main()
105 {
106     init();
107     get_sa(30);
108     get_he();
109     ffind();
110     return 0;
111 }
[POJ2774]

 

2016-07-17 16:38:16

posted @ 2016-07-17 16:32  konjak魔芋  阅读(233)  评论(0编辑  收藏  举报