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. enumerate用法总结-Python 3

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  2. python之最强王者(11)——异常(exception)

    1.Python 异常处理 python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 异常处理: 本站Python教程会具体介绍. ...

  3. [连载]《C#通讯(串口和网络)框架的设计与实现》- 12.二次开发及应用

    目       录 第十二章     二次开发及应用... 2 12.1        项目配制... 3 12.2        引用相关组件... 4 12.3        构建主程序... 5 ...

  4. spider RPC性能测试报告

    测试环境部署结构 测试用例 类 别 说明 请求报文 194字节({"systemId":"PL","appVersion":"qq ...

  5. Atitit.数据采集器 dataspider

    Atitit.数据采集器 dataspider /atiplat_cms/src/com/attilax/WebInfoX.java  @dep http://cl.cmcher.com/thread ...

  6. Web 开发中很实用的10个效果【附源码下载】

    在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...

  7. arcgis api for js共享干货系列之一自写算法实现地图量算工具

    众所周知,使用arcgis api for js实现地图的量算工具功能,无非是调用arcgisserver的Geometry服务(http://localhost:6080/arcgis/rest/s ...

  8. 一步步实现ABAP后台导入EXCEL到数据库【1】

    在SAP的应用当中,导入.导出EXCEL文件的情况是一个常见的需求,有时候用户需要将大量数据定期导入到SAP的数据库中.这种情况下,使用导入程序在前台导入可能要花费不少的时间,如果能安排导入程序为后台 ...

  9. android环境下的即时通讯

    首先了解一下即时通信的概念.通过消息通道 传输消息对象,一个账号发往另外一账号,只要账号在线,可以即时获取到消息,这就是最简单的即使通讯.消息通道可由TCP/IP UDP实现.通俗讲就是把一个人要发送 ...

  10. 当Eclipse报版本低时的处理方法

    http://blog.sina.com.cn/s/blog_6f0c85e10100v6pv.html 更新到API12的时候出过问题,这一次难免又会出现了,不过我的版本还真全啊,哇咔咔~   这里 ...