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

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.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=;
char buf[MAXN];
int rnk[MAXN];
int tmp[MAXN];
int sa[MAXN];
int lcp[MAXN];
int len,k; bool comp(int i,int j)
{
if(rnk[i]!=rnk[j]) return rnk[i]<rnk[j];
else
{
int ri=(i+k<=len)?rnk[i+k]:-;
int rj=(j+k<=len)?rnk[j+k]:-;
return ri<rj;
}
} void getsa()
{
memset(rnk,,sizeof(rnk));
memset(tmp,,sizeof(tmp));
memset(sa,,sizeof(sa));
len=strlen(buf); for(int i=;i<len;i++)
{
sa[i]=i;
rnk[i]=buf[i]-'a';
}
sa[len]=len;
rnk[len]=-; for(k=;k<=len;k*=)
{
sort(sa,sa+len+,comp); tmp[sa[]]=;
for(int i=;i<=len;i++)
{
tmp[sa[i]]=tmp[sa[i-]]+(comp(sa[i-],sa[i])?:);
} for(int i=;i<=len;i++)
{
rnk[i]=tmp[i];
}
}
} void getlcp()
{
getsa(); memset(rnk,,sizeof(rnk));
memset(lcp,,sizeof(lcp));
for(int i=;i<=len;i++)
{
rnk[sa[i]]=i;
} int h=;
lcp[]=;
for(int i=;i<len;i++)
{
int j=sa[rnk[i]-];
if(h>) h--;
for(;j+h<len&&i+h<len;h++)
if(buf[j+h]!=buf[i+h]) break; lcp[rnk[i]-]=h;
} } int T;
char ss[MAXN];
int main()
{
scanf("%d",&T);
getchar();
while(T--)
{
memset(buf,,sizeof(buf));
memset(ss,,sizeof(ss));
gets(buf);
int l=strlen(buf);
buf[l]='$';
gets(ss);
strcat(buf,ss);
getlcp(); int ans=;
for(int i=;i<len;i++)
if((sa[i]<l)!=(sa[i+]<l))
ans=max(ans,lcp[i]);
printf("Nejdelsi spolecny retezec ma delku %d.\n",ans); } return ;
}

POJ2217(最长公共子串)的更多相关文章

  1. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

  2. HDU 1503 带回朔路径的最长公共子串

    http://acm.hdu.edu.cn/showproblem.php?pid=1503 这道题又WA了好几次 在裸最长公共子串基础上加了回溯功能,就是给三种状态各做一个 不同的标记.dp[n][ ...

  3. 最长公共子序列PK最长公共子串

    1.先科普下最长公共子序列 & 最长公共子串的区别: 找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的.而最长公共子序列则并不要求连续. (1)递归方法求最长公共子序列的长度 1) ...

  4. 动态规划(一)——最长公共子序列和最长公共子串

    注: 最长公共子序列采用动态规划解决,由于子问题重叠,故采用数组缓存结果,保存最佳取值方向.输出结果时,则自顶向下建立二叉树,自底向上输出,则这过程中没有分叉路,结果唯一. 最长公共子串采用参考串方式 ...

  5. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  6. 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message

    Language: Default Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 21 ...

  7. 最长公共子串 NYOJ 36

    http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ...

  8. POJ 2217 (后缀数组+最长公共子串)

    题目链接: http://poj.org/problem?id=2217 题目大意: 求两个串的最长公共子串,注意子串是连续的,而子序列可以不连续. 解题思路: 后缀数组解法是这类问题的模板解法. 对 ...

  9. poj1159 dp最长公共子串

    //Accepted 204 KB 891 ms //dp最长公共子串 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) //dp[i][j]=max(dp[i][j],dp ...

随机推荐

  1. PHP如何学习?

    PHP 的学习,可以归纳为三个类型:      语言的基础语法学习,这些是 ifelse, while, switch, class, function, trait 等:  内置函数/类学习,这 ...

  2. python(40)- 进程、线程、协程及IO模型

    一.操作系统概念 操作系统位于底层硬件与应用软件之间的一层.工作方式:向下管理硬件,向上提供接口. 操作系统进行进程切换:1.出现IO操作:2.固定时间. 固定时间很短,人感受不到.每一个应用层运行起 ...

  3. Spring Boot 测试时的日志级别

    1.概览 该教程中,我将向你展示:如何在测试时设置spring boot 日志级别.虽然我们可以在测试通过时忽略日志,但是如果需要诊断失败的测试,选择正确的日志级别是非常重要的. 2.日志级别的重要性 ...

  4. 无法访问gcr.io的几种解决办法

    系列目录 由于一些原因,在国内无法访问gcr.io上的镜像,在安装kubernetes时,很多官方镜像又是都存在gcr.io上,在国内的一些教程中大都使用阿里云的镜像,但是由于阿里云镜像地址更换等原因 ...

  5. Effective Java - [2. 创建与销毁对象]

    让对象的创建与销毁在掌控中. Item 1: 使用静态工厂方法而非使用构造函数 public static Boolean valueOf(boolean b) { return b ? Boolea ...

  6. Linux 系统监控.诊断工具之 IO wait

    1. 常用组合方式有如下几种: 用vmstat.sar.iostat检测是否是CPU瓶颈 用free.vmstat检测是否是内存瓶颈 用iostat.dmesg 检测是否是磁盘I/O瓶颈 用netst ...

  7. 如何使Htm页面使用IE9文档模式

    修改Htm页面的方法之一是,在Head->Title下添加<META http-equiv="X-UA-Compatible" content="IE=9&q ...

  8. Sum It Up POJ 1564 HDU 杭电1258【DFS】

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  9. CentOS 7 yum安装路径查询方法

    先执行下面的命令,查看所有的已安装软件名称. rpm -qa 然后执行 rpm -ql 软件名称 就可以显示软件的安装路径.

  10. The server must be started under an unprivileged user ID to prevent

    mysql8 PostgreSQL [root@test local]# postgres -D /usr/local/pgsql/data"root" execution of ...