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 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】

    凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...

  2. springboot(三)配置日志

    github代码:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-serviceSLF ...

  3. LOJ#557. 「Antileaf's Round」你这衣服租来的吗(FHQ Treap+珂朵莉树)

    题面 传送门 题解 好吧我是不太会复杂度分析-- 我们对于每种颜色用一个数据结构维护(比方说线段树或者平衡树,代码里写的平衡树),那么区间询问很容易就可以解决了 所以现在的问题是区间修改,如果区间颜色 ...

  4. jquery 菜单展开与收缩参考脚本

    /* * metismenu - v1.1.3 * Easy menu jQuery plugin for Twitter Bootstrap 3 * https://github.com/onoku ...

  5. Java 8 (1) 行为参数化

    行为参数化就是可以帮助你处理频繁变更需求的一种软件开发模式.它意味着拿出一个代码块,把它准备好却不去执行它.这个代码块以后可以被你程序的其他部分调用,这意味着你可以推迟这块代码的执行.例如:你可以将代 ...

  6. C#知识点-GDI绘图

    一.开发环境 编译器:VS2013 .Net版本:4.5 二.开发过程 1.画一条直线 private void btnDrawLine_Click(object sender, EventArgs ...

  7. Quartz.Net学习笔记(2)-简介

    一.Quartz.Net是什么 1.来源 Quartz.Net是一个开源的作业调度框架: 2.下载地址 官网地址:http://www.quartz-scheduler.net/documentati ...

  8. 在idea中为类和方法自动生成注释

    https://my.oschina.net/mojiayi/blog/1608746

  9. linux 怎么用 名字 代替 ip ?

    比如 ssh 1.1.1.1 变成 ssh usr1 在每台机子的/etc/hosts文件中添加ip与名字的对应表

  10. Linux服务器文件权限被改

    阿里云买的ubuntu服务器遭受了不明攻击,导致站点访问不了,折腾了很久,才发现是文件的权限被修改了.然后就是一点点的修改,很是麻烦.服务器的安全要重视呢! 1.修改权限 chmod 755 * -R ...