poj2217

时间:2021-02-13 21:10:03
Secretary
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1257   Accepted: 515

Description

The basic condition of success of a political party, it is the good Election Programme. PSOS know about it, so they entrust the top secretary Juliet with this task. Because she wanted to make her work easier, she used her charm to talk round her friend Romeo to help her. Romeo is assistent of another political party and he was writing the programme some time ago. While writing the Programme for Juliet, he used some parts of his previous programme. When he gave the finished Programme to Juliet, they recognized that both programmes are too similar and that someone could notice it. They need to determine the longest part of text which is common to both programmes.

Input

At the first line there is a positive integer N stating the number of assignments to follow. Each assignment consists of exactly two lines of text, each of them contains at most 10000 characters. The end-of-line character is not considered to be a part of the text.

Output

Print a single line of text for each assignment. The line should contain the sentence "Nejdelsi spolecny retezec ma delku X." (The longest common part of text has X characters). Replace X with the length of the longest common substring of both texts.

Sample Input

2
Tady nejsou zadni mimozemstani.
Lide tady take nejsou.
Ja do lesa nepojedu.
V sobotu pojedeme na vylet.

Sample Output

Nejdelsi spolecny retezec ma delku 7.
Nejdelsi spolecny retezec ma delku 5.

白书上的题目,也看了答案。刚开始做的时候先看看,以后再自己想把。
第一道高度数组。
白书上的思路:首先想一个简化的问题:如何求出一个字符串中出现过两次以上的子串的最大长度。我们可以发现这样的子串分别是两个后缀的前缀,并且这两个后缀在后缀数组中是相邻的,因为这两个后缀的前缀相同,那么他们的排名是由长度决定的,肯定就是相邻了。
但是两个字符串就不好搞了,这时我们可以转化一下:把两个字符串拼在一起,就是直接头接尾(白书上说中间要加一个字符,我没接也对了)然后求出lcp,这时只要计算lcp的最大值就是答案了。但是注意,因为一个字符串是被拼在后面的,那么
sa[i]和sa[i-1]必须一个<s.length(),一个>=s.length()(s:第一个字符串),才能满足条件,为什么呢,因为我们求的是这两个字符串中出现的子串,所以当然sa必须一前一后了
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 20010
int n,ans,k;
int sa[N],lcp[N],rank[N],temp[N];
string s,t,S;
bool cp(int i,int j)
{
if(rank[i]!=rank[j]) return rank[i]<rank[j];
int ri=i+k<=n?rank[i+k]:-;
int rj=j+k<=n?rank[j+k]:-;
return ri<rj;
}
void Sa(string s)
{
n=s.length()-;
for(int i=;i<=n;i++)
{
rank[i]=s[i]; sa[i]=i;
}
for(k=;k<=n;k*=)
{
sort(sa,sa+n+,cp);
temp[sa[]]=;
for(int i=;i<=n;i++)
{
temp[sa[i]]=temp[sa[i-]]+(cp(sa[i-],sa[i]));
}
for(int i=;i<=n;i++) rank[i]=temp[i];
}
}
void Lcp(string s)
{
n=s.length()-;
for(int i=;i<=n;i++) rank[sa[i]]=i;//位置为sa[i]rank为i
lcp[]=;
int h=;
for(int i=;i<=n;i++)
{
int j=sa[rank[i]-];//这个位置的rank为i,上一个地方的位置则为rank-1,sa存的是排名对应的位置
if(h>) h--;
while(s[i+h]==s[j+h]&&i+h<=n&&j+h<=n) h++;
lcp[rank[i]-]=h;
}
}
int main()
{
int T; scanf("%d",&T);
cin.ignore();
while(T--)
{
memset(sa,,sizeof(sa));
ans=;
getline(cin,s,'\n');
getline(cin,t,'\n');
S=s+t;
int ls=s.length();
Sa(S); Lcp(S);
// printf("\n-------------\n");
for(int i=;i<S.length();i++)
{
// printf("lcp=%d\n",lcp[i-1]);
if((sa[i]>=ls&&sa[i-]<ls)||(sa[i]<ls&&sa[i-]>=ls))
ans=max(ans,lcp[i-]);
}
printf("Nejdelsi spolecny retezec ma delku %d.\n",ans);
}
return ;
}