POJ 2217 Secretary
Secretary
This problem will be judged on PKU. Original ID: 2217
64-bit integer IO format: %lld Java class name: Main
Input
Output
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
#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的更多相关文章
- 后缀数组 POJ 2217 Secretary
题目链接 题意:求两个字符串的最长公共子串 分析:做法是构造新的串是两个串连接而成,中间用没有出现的字符隔开(因为这样才能保证S的后缀的公共前缀不会跨出一个原有串的范围),即newS = S + '$ ...
- POJ 2217 Secretary (后缀数组)
标题效果: 计算两个公共串串最长的字符串的长度. IDEAS: 这两个组合的字符串. 然后直接确定运行后缀数组height 然后,你可以直接扫描一次height .加个是不是在一个串中的推断就能够了. ...
- POJ 2217:Secretary(后缀数组)
题目大意:求两个字符串的公共子串. 分析: 模板题,将两个字符串接起来用不会出现的字符分割,然后求分属两个字符串的相邻后缀lcp的最大值即可. 代码: program work; type arr=. ...
- POJ 2217 (后缀数组+最长公共子串)
题目链接: http://poj.org/problem?id=2217 题目大意: 求两个串的最长公共子串,注意子串是连续的,而子序列可以不连续. 解题思路: 后缀数组解法是这类问题的模板解法. 对 ...
- POJ 2217 LCS(后缀数组)
Secretary Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1655 Accepted: 671 Descript ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
随机推荐
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】
凸包模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> ...
- springboot(三)配置日志
github代码:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-serviceSLF ...
- LOJ#557. 「Antileaf's Round」你这衣服租来的吗(FHQ Treap+珂朵莉树)
题面 传送门 题解 好吧我是不太会复杂度分析-- 我们对于每种颜色用一个数据结构维护(比方说线段树或者平衡树,代码里写的平衡树),那么区间询问很容易就可以解决了 所以现在的问题是区间修改,如果区间颜色 ...
- jquery 菜单展开与收缩参考脚本
/* * metismenu - v1.1.3 * Easy menu jQuery plugin for Twitter Bootstrap 3 * https://github.com/onoku ...
- Java 8 (1) 行为参数化
行为参数化就是可以帮助你处理频繁变更需求的一种软件开发模式.它意味着拿出一个代码块,把它准备好却不去执行它.这个代码块以后可以被你程序的其他部分调用,这意味着你可以推迟这块代码的执行.例如:你可以将代 ...
- C#知识点-GDI绘图
一.开发环境 编译器:VS2013 .Net版本:4.5 二.开发过程 1.画一条直线 private void btnDrawLine_Click(object sender, EventArgs ...
- Quartz.Net学习笔记(2)-简介
一.Quartz.Net是什么 1.来源 Quartz.Net是一个开源的作业调度框架: 2.下载地址 官网地址:http://www.quartz-scheduler.net/documentati ...
- 在idea中为类和方法自动生成注释
https://my.oschina.net/mojiayi/blog/1608746
- linux 怎么用 名字 代替 ip ?
比如 ssh 1.1.1.1 变成 ssh usr1 在每台机子的/etc/hosts文件中添加ip与名字的对应表
- Linux服务器文件权限被改
阿里云买的ubuntu服务器遭受了不明攻击,导致站点访问不了,折腾了很久,才发现是文件的权限被修改了.然后就是一点点的修改,很是麻烦.服务器的安全要重视呢! 1.修改权限 chmod 755 * -R ...