Secretary

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2217
64-bit integer IO format: %lld      Java class name: Main

 
 
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

 
解题:后缀数组。将两个串S,T链接起来。然后求lcp.sa[i]和sa[i+1]不同时在S或者不同时在T中,求满足这样条件的最大lcp。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int n,k,_rank[maxn],sa[maxn],lcp[maxn],tmp[maxn];
char ss[];
bool cmp_sa(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 construct_sa(char *S,int *sa){
for(int i = ; i <= n; i++){
sa[i] = i;
_rank[i] = i < n ? S[i]:-;
}
for(k = ; k <= n; k <<= ){
sort(sa,sa+n+,cmp_sa);
tmp[sa[]] = ;
for(int i = ; i <= n; i++)
tmp[sa[i]] = tmp[sa[i-]] + cmp_sa(sa[i-],sa[i]);
for(int i = ; i <= n; i++)
_rank[i] = tmp[i];
}
}
void construct_lcp(char *S,int *lcp){
for(int i = ; i <= n; i++) _rank[sa[i]] = i;
int h = lcp[] = ;
for(int i = ; i < n; i++){
if(h) h--;
for(int j = sa[_rank[i]-]; i+h < n && j+h < n && S[i+h] == S[j+h]; ++h);
lcp[_rank[i]-] = h;
}
}
int main() {
int t,slen;
scanf("%d",&t);
getchar();
while(t--){
gets(ss);
slen = strlen(ss);
ss[slen] = '#';
gets(ss+slen+);
n = strlen(ss);
memset(sa,,sizeof(sa));
memset(lcp,,sizeof(lcp));
construct_sa(ss,sa);
construct_lcp(ss,lcp);
int ans = ;
for(int i = ; i < n; i++)
if((sa[i] < slen) != (sa[i+] < slen)) ans = max(ans,lcp[i]);
printf("Nejdelsi spolecny retezec ma delku %d.\n",ans);
}
return ;
}

POJ 2217 Secretary的更多相关文章

  1. 后缀数组 POJ 2217 Secretary

    题目链接 题意:求两个字符串的最长公共子串 分析:做法是构造新的串是两个串连接而成,中间用没有出现的字符隔开(因为这样才能保证S的后缀的公共前缀不会跨出一个原有串的范围),即newS = S + '$ ...

  2. POJ 2217 Secretary (后缀数组)

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

  3. POJ 2217:Secretary(后缀数组)

    题目大意:求两个字符串的公共子串. 分析: 模板题,将两个字符串接起来用不会出现的字符分割,然后求分属两个字符串的相邻后缀lcp的最大值即可. 代码: program work; type arr=. ...

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

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

  5. POJ 2217 LCS(后缀数组)

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

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  8. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  9. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

随机推荐

  1. bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏【dfs】

    这个可以用异或高斯消元,但是我不会呀我用的暴搜 2的m次方枚举第一行的翻转情况,然后后面的就定了,因为对于一个j位置,如果i-1的j位置需要翻,那么一定要翻i的j,因为这是i-1的j最后翻的机会 按字 ...

  2. bzoj 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课【spfa】

    洛谷的数据毒啊 把(i,j,k)作为一个点spfa,表示点(i,j)朝向k方向,然后向四个方向转移即可 #include<iostream> #include<cstdio> ...

  3. bzoj 2442: [Usaco2011 Open]修剪草坪【单调栈】

    设f[i]为i不选的最小损失,转移是f[i]=f[j]+e[i[(i-j-1<=k) 因为f是单调不降的,所以f[j]显然越靠右越好因为i-j-1<=k的限制,所以单调栈需要弹栈 #inc ...

  4. 虚拟机安装cenos7后ifcfg看网卡无inet地址掩码等信息

    在虚拟机安装centos7,进入系统使用ifconfig命令时,只有lo网卡( 127.0.0.1的ip地址)和eno16777736网卡,而且此网卡没有inet地址.掩码等信息. 这时候查看/etc ...

  5. Linux学习笔记之Linux相关知识

    [想成为某一方面的大神,没有捷径可走,只能不断的记录.练习.总结.coding……] notes:主要从网上摘录了一些关于Linux的历史以及一些相关内容,以便对Linux系统有一定的了解.这不但可以 ...

  6. POJ 3608 旋转卡壳

    思路: 旋转卡壳应用 注意点&边  边&边  点&点 三种情况 //By SiriusRen #include <cmath> #include <cstdi ...

  7. Spring Cloud学习(一)

    SpringCloud是什么? Spring Cloud是一个微服务框架,相比Dubbo等RPC框架, Spring Cloud提供的全套的分布式系统解决方案. Spring Cloud对微服务基础框 ...

  8. PS如何批量整理图片大下

    https://jingyan.baidu.com/article/cdddd41cc7849853cb00e193.html

  9. 前端--2、CSS基础

    CSS的部分: CSS四种类引入方式(了解) style的定义原则: 基本选择器 示例: 层级选择器 组合选择器 后代选择器 ★ 子代选择器 毗邻选择器 普通兄弟选择器 “与”选择器 ★ “或”选择器 ...

  10. Android RecyclerView局部刷新那个坑

    关键:public final void notifyItemChanged(int position, Object payload) RecyclerView局部刷新大家都遇到过,有时候还说会遇见 ...