【POJ 2774】Long Long Message 最长公共子串
还是模板啊,手残&&打成||查错查了1h+TAT
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 2000003; int t1[N], t2[N], c[N];
void st(int *x, int *y, int *sa, int n, int m) {
int i;
for(i = 0; i < m; ++i) c[i] = 0;
for(i = 0; i < n; ++i) ++c[x[y[i]]];
for(i = 1; i < m; ++i) c[i] += c[i - 1];
for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
}
void mkhz(int *a, int *sa, int n, int m) {
int i, j, p, *t, *x = t1, *y = t2;
for(i = 0; i < n; ++i) x[i] = a[i], y[i] = i;
st(x, y, sa, n, m);
for(p = 1, j = 1; p < n; j <<= 1, m = p) {
for(p = 0, i = n - j; i < n; ++i) y[p++] = i;
for(i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
st(x, y, sa, n, m);
for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)
x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + j] == y[sa[i - 1] + j] ? p - 1 : p++;
}
}
void mkh(int *r, int *sa, int *rank, int *h, int n) {
int i, j, k = 0;
for(i = 1; i <= n; ++i) rank[sa[i]] = i;
for(i = 1; i <= n; h[rank[i++]] = k)
for(k ? --k : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k);
} char s[N];
int a[N], sa[N], h[N], rank[N], n;
int main() {
while(~scanf("%s", s + 1)) {
int tmp = strlen(s + 1);
s[tmp + 1] = '$';
scanf("%s", s + tmp + 2);
int n = strlen(s + 1);
for(int i = 1; i <= n; ++i) a[i] = s[i];
mkhz(a, sa, n + 1, 130);
mkh(a, sa, rank, h, n);
int ans = 0;
++tmp;
for(int i = 2; i <= n; ++i)
if (((tmp < sa[i - 1] && tmp > sa[i]) || (tmp < sa[i] && tmp > sa[i - 1])) && h[i] > ans)
ans = h[i];
printf("%d\n", ans);
}
return 0;
}
Orz
【POJ 2774】Long Long Message 最长公共子串的更多相关文章
- POJ 2774 Long Long Message [ 最长公共子串 后缀数组]
题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- POJ 3450 Corporate Identity kmp+最长公共子串
枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...
- POJ 2774 Long Long Message (二分 + Hash 求最长公共子串)题解
题意:求最长公共子串 思路:把两个串Hash,然后我们把短的作为LCS的最大可能值,然后二分长度,每次判断这样二分可不可以.判断时,先拿出第一个母串所有len长的子串,排序,然后枚举第二个母串len长 ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- poj 2774 最长公共子串 后缀数组
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 25752 Accepted: 10 ...
- poj 2774 后缀数组 两个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 31904 Accepted: 12 ...
随机推荐
- [cocos2d-x]深入--几个代表性的类 (续)
摘要: 此文对cocos2d-x引擎中最具代表性,最能体现框架结构的几个类做了简单的介绍, 包括Director,Application, Renderer, EventDispatcher, Sch ...
- Zookeeper C API 指南三(回调函数)(转)
2013-02-21 12:54 by Haippy, 9237 阅读, 0 评论, 收藏, 编辑 接上一篇<Zookeeper C API 指南二(监视(Wathes), 基本常量和结构体介绍 ...
- Thread 同步线程(打印机同步)
1.首先创建一个打印机对象 package cn.b.happy; public class Printer { Object o =new Object(); public void print() ...
- 虚拟机VMware怎么完全卸载干净
虚拟机VMware怎么完全卸载干净 听语音 | 浏览:19929 | 更新:2014-12-21 10:28 | 标签:虚拟机 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师傅高质屏和好师傅 ...
- java 25 - 4 网络编程之 UDP协议传输的代码优化
UDP协议的输出端: /* UDP发送数据: A:创建Socket发送端对象 B:创建数据报包(把数据打包) C:调用Socket对象发送数据报包 D:释放资源(底层是IO流) */ public c ...
- 使用adb shell卸载程序
个人感觉在命令行中卸载程序要比在手机界面卸载程序要方便许多,配合命令行下的报名查看包名的命令就更加方便了. 1.查看应用准确包名 adb shell pm list package -f |grep ...
- css3动画的两种方式transition和@keyframs
- Java daemon thread 守护线程
守护线程与普通线程写法上基本么啥区别,在启动线程前, 调用线程对象的方法setDaemon(true),则可以将其设置为守护线程. 守护线程使用的情况较少,但并非无用,举例来说,JVM的垃圾回收.内存 ...
- some basic graph theoretical measures
· mean characteristic path length calculated as the average length of the shortest path between two ...
- 05传智_jbpm与OA项目_部门模块中增加部门的jsp页面增加一个在线编辑器功能
这篇文章讲的是在线编辑器功能,之前的部门模块中,增加部门的功能jsp页面起先是这么做的.