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. 利用VideoView播放视频

    package com.qianhua.ui; 002   003 import android.app.Activity; 004 import android.content.Intent; 00 ...

  2. host dig nslookup bind

    这三个工具包含在yum install bind-utils -y dig -t mx|ns|A baidu.com qq.com dig -x 113,11.2.11 http://www.cnbl ...

  3. 使用match、test控制输入字符格式后键盘向左向右键光标自动定位解决

    直接举例说明(正则表达式替换红色区域即可) /** * 判断是否位数字 * @param obj 数字 */ function numOnly(obj) { if(!(/^\d*$/.test(obj ...

  4. mysql大数据量下修改表结构的方法

    http://www.blogjava.net/anchor110/articles/361152.html

  5. window 平台搭建简单的直播点播系统

    Windows平台如何搭建简单的直播系统前文已经有介绍,今天介绍下如何搭建简单的点播系统. 同样还是利用crtmpServer,crtmpServer可以从github中下载,可以从群里下载(群里有修 ...

  6. Thunderbolt雷电接口

    官网:https://thunderbolttechnology.net/tech/certification

  7. Android开源git40个App源代码

    (JamsMusicPlayer)非常棒的音乐播放器(new)   (F8)日程安排的软件   (Conversations)基于XMPP的应用   (Bitocle)能够在手机上查看自己github ...

  8. Canvas学习笔记——动画中的三角学

    示例1,跟随鼠标的键头:   需要掌握一个重要的公式,这个方法返回从 x 轴到点 (x,y) 之间的角度 Math.atan2(dy,dx); 关键代码: function Arrow() { thi ...

  9. 03 svn 权限与用户管理

    一:权限管理 (1)svn仓库各个作用 svnserve.conf [svn仓库的配置文件] password [svn仓库账号和密码配置文件] authz [svn仓库的访问权限] (2)访问权限 ...

  10. linux的su和sudo(转载)

    来源:http://www.jb51.net/LINUXjishu/12713.html 一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比 ...