Secretary
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1655   Accepted: 671

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.

Source

题意:求LCS长度。
代码:
 //#include"bits/stdc++.h"
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"ctime"
#include"iostream"
#include"cstdlib"
#include"algorithm"
#define db double
#define ll long long
#define ull unsigned long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
int sa[N];
int rk[N];
int tmp[N];
int lcp[N];
int n,k,T;
bool cmp(int i,int j){
if(rk[i] != rk[j]) return rk[i]<rk[j];
else
{
int ri=i+k<=n?rk[i+k]:-;
int rj=j+k<=n?rk[j+k]:-;
return ri<rj;
}
}
void bulid(string s,int *sa)
{
n=(int)s.size();
for(int i=;i<=n;i++){
sa[i]=i;
rk[i]=i<n?s[i]:-;
}
for(k=;k<=n;k*=){
sort(sa,sa+n+,cmp);
tmp[sa[]]=;
for(int i=;i<=n;i++){
tmp[sa[i]]=tmp[sa[i-]]+(cmp(sa[i-],sa[i])?:);
}
for(int i=;i<=n;i++){
rk[i]=tmp[i];
}
}
}
void LCP(string s,int *sa,int *lcp){
n=(int)s.size();
for(int i=;i<=n;i++) rk[sa[i]]=i;
int h=;
lcp[]=;
for(int i=;i<n;i++){
int j=sa[rk[i]-];
for (h ? h-- : ; j + h < n&&i + h < n&&s[j + h] == s[i + h]; h++);
lcp[rk[i]-] = h;
}
}
int main()
{
ios::sync_with_stdio();
cin>>T;
getchar();
while(T--)
{
string s,t;
getline(cin,s);//read a line
getline(cin,t);
int s1=(int)s.size();
s+='$'+t;
n=(int)s.size();
bulid(s,sa);
LCP(s,sa,lcp);
int ma=;
for(int i = ;i < n; i++){
if((sa[i]<s1)!=(sa[i+]<s1)) ma=max(ma,lcp[i]);
}
printf("Nejdelsi spolecny retezec ma delku %d.\n",ma);
}
return ;
}

POJ 2217 LCS(后缀数组)的更多相关文章

  1. POJ 2217 Secretary (后缀数组)

    标题效果: 计算两个公共串串最长的字符串的长度. IDEAS: 这两个组合的字符串. 然后直接确定运行后缀数组height 然后,你可以直接扫描一次height .加个是不是在一个串中的推断就能够了. ...

  2. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

  3. POJ 1743-POJ - 3261~后缀数组关于最长字串问题

    POJ 1743 题意: 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1~~88范围内的整数,现在要找一个重复的主题.“主题”是整个音符序列的一个子串,它需 ...

  4. POJ - 1226 Substrings (后缀数组)

    传送门:POJ - 1226 这个题跟POJ - 3294  和POJ - 3450 都是一样的思路,一种题型. POJ - 3294的题解可以见:https://www.cnblogs.com/li ...

  5. Maximum repetition substring(POJ - 3693)(sa(后缀数组)+st表)

    The repetition number of a string is defined as the maximum number \(R\) such that the string can be ...

  6. Milk Patterns POJ - 3261(后缀数组+二分)

    题意: 求可重叠的最长重复子串,但有一个限制条件..要至少重复k次 解析: 二分枚举k,对于连续的height 如果height[i] >= k 说明它们至少有k个元素是重复的,所以判断一下就好 ...

  7. POJ 3581 Sequence(后缀数组)

    Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to ...

  8. POJ 3581 Sequence ——后缀数组 最小表示法

    [题目分析] 一见到题目,就有了一个显而易见obviously的想法.只需要每次找到倒过来最小的那一个字符串翻转就可以了. 然而事情并不是这样的,比如说505023这样一个字符串,如果翻转了成为320 ...

  9. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

随机推荐

  1. maven课程 项目管理利器-maven 3-10 maven聚合和继承 4星

    本节主要讲了以下内容: 1 maven聚合 2 maven继承 1 maven聚合 <!-- 聚合特有标签 --> <groupId>com.hongxing</grou ...

  2. ArrayList、Vector、HashMap、HashSet

    1. list  和 set 的有序无序是否可重复 List是有序可重复的: Set是无序不可重复的: 2.ArrayList.Vector.HashMap.HashSet的默认初始容量.加载因子.扩 ...

  3. 微信小程序实战篇:基于wxcharts.js绘制移动报表

    前言 微信小程序图表插件(wx-charts)是基于canvas绘制,体积小巧,支持图表类型饼图.线图.柱状图 .区域图等图表图形绘制,目前wx-charts是微信小程序图表插件中比较强大好使的一个. ...

  4. 学习路线 roadmap

    我的学习路线为HTML > CSS > Javsscript:Javascript是前端一切学习的基础.HTML和css一起学习. JavaScript基础: Js基础教程.js内置对象常 ...

  5. 移动web基础

    接触retina屏 基础知识(移动Web的基础知识)排版布局(高效的移动Web布局)开发效率终端交互优化 pixel像素基础viewport视图viewport_meta标签viewport_codi ...

  6. python3绘图示例4(基于matplotlib:箱线图、散点图等)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from matplotlib.pyplot import * x=[1,2,3,4]y=[5,4,3,2] # ...

  7. CentOS7.3下关于DHCP中继代理服务器的详细配置

    DHCP服务器只作用于局域网同一网段内,客户端是通过广播消息来获得DHCP服务器响应后才能得到IP地址的,但广播消息不能跨越子网,那么如何让客户端获取到DHCP服务器提供的IP地址呢?这就是DHCP中 ...

  8. java多线程安全

    class Ticket implements Runnable { public int sum=10; public void run() { while(true) { if(sum>0) ...

  9. 寄生构造函数模式 js

    有一点需要说明:首先返回的对象与构造函数或者构造函数的原型属性之间没有关系,也就是说构造函数返回的对象与在构造函数外部创建的对象没有什么不同,为此不能依赖 instanceof 操作符来确定对象类型. ...

  10. react里面引入图片

    引入图片确实走了很多的坑,开始的认知里,所有静态页面都需要放在public里面能执行,代码是这样的 css .back{ background-image: url('/images/homeBack ...