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 ;
}

poj2217的更多相关文章

  1. POJ2217 Secretary 后缀数组&&高度数组

    学后缀数组后的一道裸题.先来讲讲收获,作为字符串初学者,后缀数组也是刚刚在学,所幸的是有一篇好的论文<后缀数组--处理字符串的有力工具>by 罗穗骞,里面非常详尽地介绍了有关后缀数组的概念 ...

  2. POJ2217(最长公共子串)

    Secretary Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 992   Accepted: 408 Descripti ...

  3. poj2217 Secretary 后缀数组

    #include <iostream> #include <cstring> #include <string> #include <cstdio> u ...

随机推荐

  1. HTML课上小结

    HTML翻译为超文本标记语言<标签名>内容</标签名>静态网页动态网页的区别是看是否从数据中提取数据一般网页由几部分组分组成<html>开始标签 <head& ...

  2. JavaWeb_day07_JSP

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day07 JSP 全称 :Java Server P ...

  3. 智软科技医疗器械GSP监管软件通过多省市药监局检查

    提供医疗器械GSP监管软件,通过多省市药监局检查,符合2016年最新GSP监管条例的要求. 企业客户列表 温岭市万悦医疗器械有限公司 杭州市上善医疗器械有限公司 武汉明德生物科技股份有限公司 http ...

  4. Java名字的由来

    Java语言的历程丰富多彩,被现在众多程序员和企业广泛使用,不用质疑这是Java的领先技术的结果. Java是Sun公司开发的一种编程语言,Sun公司最初的方向是让Java来开发一些电器装置程序,如: ...

  5. JQuery中$.ajax()方法参数详解

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

  6. Bootstrap之导航条

    基本导航条 <!-- navbar-inverse相反颜色风格 --> <!-- navbar-static-top去除圆角 --> <!-- navbar-fixed- ...

  7. 使用IdleTest进行TDD单元测试驱动开发演练(3) 之 ASP.NET MVC

    一.[前言] (1)本文将用到IOC框架Unity,可参照<Unity V3 初步使用 —— 为我的.NET项目从简单三层架构转到IOC做准备>(2)本文的解决方案是基于前述<使用I ...

  8. Android开发学习—— 消息队列

    ###主线程不能被阻塞* 在Android中,主线程被阻塞会导致应用不能刷新ui界面,不能响应用户操作,用户体验将非常差* 主线程阻塞时间过长,系统会抛出ANR异常* ANR:Application ...

  9. ITIS-资料集合贴

    ITIS-资料集合贴 说明:这个贴用于收集笔者能力范围内收集收藏并认为有用的资料,方便各方参考,免去到处找寻之苦,提升信息的交叉引用价值.另外,笔者就自己感悟做了部分评注,且可能尝试不断的优化分类和排 ...

  10. iOS系列 基础篇 04 探究视图生命周期

    iOS系列 基础篇 04 探究视图生命周期 视图是应用的一个重要的组成部份,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻. 以视图的四种状态为基础,我们来系统了解一下视 ...