POJ2217——Secretary(后缀数组应用)

Secretary
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1089 Accepted: 446

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,即后缀数组中相邻后缀的最大公共前缀。

那么如果我们把这两个字符串合并成一个,就相当于求上面那个问题了。为了区分两个字符串的后缀,我们在其中加入一个不会出现的字符即可。再求后缀数组和高度数组。


#include<cstdio>
#include<cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 10010

int n,k;
int rank[MAXN+1];
int tmp[MAXN+1];

//比较(rank[i],rank[i+k])和(rank[j],rank[j+k])
bool compare_sa(int i,int j){
	if(rank[i]!=rank[j])
		return rank[i]<rank[j];
	else{
		int ri=i+k<=n?rank[i+k]:-1;
		int rj=j+k<=n?rank[j+k]:-1;
		return ri<rj;
	}
}
//rank用来记录字符串的排序,sa用来记录开头字符的位置,S用来记录字符串
//第一个通常是空字符串
//计算字符串S的后缀数组
void construct_sa(string S,int *sa){
	n=S.length();

	//初始长度为1,rank直接取字符的编码.
	for(int i=0;i<=n;i++){
		sa[i]=i;
		rank[i]=i<n?S[i]:-1;
	}

	//利用对长度为k的排序的结果对长度为2k的排序
	for(k=1;k<=n;k*=2){
		sort(sa,sa+n+1,compare_sa);

		//先在tmp中临时储存新计算的rank,再转存回rank中
		tmp[sa[0]]=0;
		for(int i=1;i<=n;i++){
			tmp[sa[i]]=tmp[sa[i-1]]+(compare_sa(sa[i-1],sa[i])?1:0);
		}
		for(int i=0;i<=n;i++){
			rank[i]=tmp[i];
		}
	}
}


//高度数组lcp的计算
void construct_lcp(string S,int *sa,int *lcp){
	int n=S.length();
	for(int i=0;i<=n;i++) rank[sa[i]]=i;

	int h=0;
	lcp[sa[0]]=0;
	for(int i=0;i<n;i++){
		//计算字符串中从位置i开始的后缀及其在后缀数组中的前一个后缀的lcp
		int j=sa[rank[i]-1];

		//将h先减去首字母的1长度,在保持前缀相同的前提下不断地增加
		if(h>0) h--;
		for(;j+h<n&&i+h<n;h++){
			if(S[j+h]!=S[i+h]) break;
		}
		lcp[rank[i]-1]=h;
	}
}

string S,T;
int sa[MAXN],lcp[MAXN];

void solve(){
    int sl=S.length();
    S+='$'+T;

    construct_sa(S,sa);
    construct_lcp(S,sa,lcp);

    int ans=0;
    for(int i=0;i<S.length();i++){
        if((sa[i]<sl)!=(sa[i+1]<sl)){
            ans=max(ans,lcp[i]);
        }
    }
    cout << "Nejdelsi spolecny retezec ma delku " << ans << "." << endl;
}
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        memset(sa,0,sizeof(sa));
        memset(tmp,0,sizeof(tmp));
        memset(rank,0,sizeof(rank));
        memset(lcp,0,sizeof(lcp));
        getline(cin,S);
        getline(cin,T);
        solve();
    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值