[POJ2774][codevs3160]Long Long Message

试题描述

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother.

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out:

1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different.

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat.

Background: 
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be.

Why ask you to write a program? There are four resions: 
1. The little cat is so busy these days with physics lessons; 
2. The little cat wants to keep what he said to his mother seceret; 
3. POJ is such a great Online Judge; 
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

输入

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

输出

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

输入示例

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

输出示例


数据规模及约定

见“输入

题解

两个串拼在一起(中间别忘了加分隔符)跑一边后缀排序求出 height 数组,然后二分答案,对于 height[i] ≥ x 的分为一组,然后 check 时看一组内是否两个串的后缀都出现就行了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; #define maxn 200010 char S[maxn], S2[maxn];
int n, rank[maxn], height[maxn], sa[maxn], Ws[maxn];
bool cmp(int* a, int p1, int p2, int l) {
if(p1 + l > n && p2 + l > n) return a[p1] == a[p2];
if(p1 + l > n || p2 + l > n) return 0;
return a[p1] == a[p2] && a[p1+l] == a[p2+l];
}
void ssort() {
int *x = rank, *y = height;
int m = 0;
for(int i = 1; i <= n; i++) Ws[x[i] = S[i]]++, m = max(m, x[i]);
for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
for(int i = n; i; i--) sa[Ws[x[i]]--] = i;
for(int j = 1, pos = 0; pos < n; j <<= 1, m = pos) {
pos = 0;
for(int i = n - j + 1; i <= n; i++) y[++pos] = i;
for(int i = 1; i <= n; i++) if(sa[i] > j) y[++pos] = sa[i] - j;
for(int i = 1; i <= m; i++) Ws[i] = 0;
for(int i = 1; i <= n; i++) Ws[x[i]]++;
for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
for(int i = n; i; i--) sa[Ws[x[y[i]]]--] = y[i];
swap(x, y); pos = 1; x[sa[1]] = 1;
for(int i = 2; i <= n; i++) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? pos : ++pos;
}
return ;
}
void calch() {
for(int i = 1; i <= n; i++) rank[sa[i]] = i;
for(int i = 1, j, k = 0; i <= n; height[rank[i++]] = k)
for(k ? k-- : 0, j = sa[rank[i]-1]; S[j+k] == S[i+k]; k++);
return ;
} bool check(int x, int p) {
bool fl = 0, fr = 0;
if(sa[1] <= p) fl = 1; else fr = 1;
for(int i = 2; i <= n; i++) {
if(height[i] < x) {
fl = fr = 0;
if(sa[i] <= p) fl = 1; else fr = 1;
}
else {
if(sa[i] <= p) fl = 1; else fr = 1;
if(fl & fr) return 1;
}
}
return 0;
} int main() {
scanf("%s%s", S + 1, S2 + 1); n = strlen(S + 1); int m = strlen(S2 + 1);
for(int i = 1; i <= m; i++) S[n+i+1] = S2[i];
S[n+1] = '#';
swap(m, n);
n += m + 1;
ssort();
calch(); int l = 0, r = n + 1;
while(r - l > 1) {
int mid = l + r >> 1;
if(check(mid, m)) l = mid; else r = mid;
}
printf("%d\n", l); return 0;
}

建出第一个串的 SAM 后用第二个串在上面匹配。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; #define maxn 200010
#define maxa 26 char S[maxn];
int len; int rt, ToT, last, to[maxn][maxa], par[maxn], Max[maxn];
void extend(int x) {
int p = last, np = ++ToT; Max[np] = Max[p] + 1; last = np;
while(p && !to[p][x]) to[p][x] = np, p = par[p];
if(!p){ par[np] = rt; return ; }
int q = to[p][x];
if(Max[q] == Max[p] + 1){ par[np] = q; return ; }
int nq = ++ToT; Max[nq] = Max[p] + 1;
memcpy(to[nq], to[q], sizeof(to[q]));
par[nq] = par[q];
par[q] = par[np] = nq;
while(p && to[p][x] == q) to[p][x] = nq, p = par[p];
return ;
} int main() {
rt = ToT = last = 1;
scanf("%s", S);
len = strlen(S);
for(int i = 0; i < len; i++) extend(S[i] - 'a'); scanf("%s", S);
int p = rt, n = strlen(S), tmp = 0, ans = 0;
for(int i = 0; i < n; i++) {
int x = S[i] - 'a';
if(to[p][x]) tmp++, p = to[p][x];
else {
while(p && !to[p][x]) tmp = Max[par[p]] + 1, p = par[p];
if(!p) tmp = 0, p = 1;
else p = to[p][x];
}
ans = max(ans, tmp);
} printf("%d\n", ans); return 0;
}

[POJ2774][codevs3160]Long Long Message的更多相关文章

  1. 【POJ2774】Long Long Message(后缀数组)

    [POJ2774]Long Long Message(后缀数组) 题面 Vjudge Description Little cat在Byterland的首都读物理专业.这些天他收到了一条悲伤地信息:他 ...

  2. 【POJ2774】Long Long Message (后缀数组)

    Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A p ...

  3. 【poj2774】 Long Long Message

    http://poj.org/problem?id=2774 (题目链接) 题意 给出两个只包含小写字母的字符串,求出最长连续公共子串. solution 第一次用后缀数组,感觉有点神...才发现原来 ...

  4. HiHocoder1415 : 后缀数组三·重复旋律3 & Poj2774:Long Long Message

    题面 HiHocoder1415 Poj2774 Sol 都是求最长公共子串,\(hihocoder\)上讲的很清楚 把两个串拼在一起,中间用一个特殊字符隔开 那么答案就是排序后相邻两个不同串的后缀的 ...

  5. POJ2774:Long Long Message——题解

    http://poj.org/problem?id=2774 给定两个字符串 A 和 B,求最长公共子串. 论文题,把两个串合并起来,比较两个串各自的后缀的height值取最大即可. #include ...

  6. 【POJ2774】Long Long Message(后缀数组求Height数组)

    点此看题面 大致题意: 求两个字符串中最长公共子串的长度. 关于后缀数组 关于\(Height\)数组的概念以及如何用后缀数组求\(Height\)数组详见这篇博客:后缀数组入门(二)--Height ...

  7. 【POJ2774】Long Long Message (SA)

    最长公共子串...两个字符串连在一起,中间放一个特殊字符隔开.求出height之后,枚举height,看两个后缀是不是分布于两段字符串..如果是,这个值就可以作为答案.取最大值即可. ; var c, ...

  8. 【poj2774】Long Long Message

    用个分隔符将两个字符串连接起来,再用后缀数组求出height数组的值,找出一个height值最大并且i与i-1的sa值分别在两串字符中就好 #include<algorithm> #inc ...

  9. POJ2774:Long Long Message

    问两个串的最长公共子串,n<=100000. SAM可以直接搞当然SA哈希都可以..类似于KMP的做法,如果沿parent边走要顺势修改匹配位置. #include<stdio.h> ...

随机推荐

  1. JEECMSv9.3在金蝶Apusic中间件中无法找到首页的问题处理

    在金蝶中间件中启动JEECMS,访问首页显示"页面找不到"信息.而访问后台及其他页面均可正常访问. 经代码查找,发现前台页面的所有地址是通过"com.jeecms.cms ...

  2. [转]MySQL游标的使用

    转自:http://www.cnblogs.com/sk-net/archive/2011/09/07/2170224.html 以下的文章主要介绍的是MySQL游标的使用笔记,其可以用在存储过程的S ...

  3. jmeter正则表达式提取器使用

    引用名称:请求中的参数需要引用的名称 正则表达式:从结果集中提取数据,例如从数据库查询结果中提取数据 模板:$1$表示提取表达式中的第一个值,$n$以此类推 匹配数字:0代表随机,1代表第一个值,n代 ...

  4. Oracle分区表例子

    分区表 Oracle提供的分区方法有以下几种. 1.范围分区(range) 范围分区是应用范围比较广的表分区方式,它是以列的值的范围来作为分区的划分条 件,将记录存放到列值所在的 range分区中. ...

  5. js基础 -----鼠标事件(按下 拖拽)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 【转】Nicescroll滚动条插件的用法

    原网址:http://blog.csdn.net/mss359681091/article/details/52838179 Nicescroll滚动条插件是一个非常强大的基于JQUERY的滚动条插件 ...

  7. 一些常用的meta标签及其作用

    声明文档使用的字符编码  <meta charset='utf-8'>优先使用 IE 最新版本和 Chrome  <meta http-equiv="X-UA-Compat ...

  8. WebSocket 的一些简单页面推送使用

    因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...

  9. 半斤八两中级破解 (四) TCP_UDP协议转向本地验证

    首先要用抓包工具判断是哪种协议,根据封包助手来看,教程中给出的例子是个TCP协议的,此时要记录下包的: 源地址,源端口     目的地址,目的端口   源包大小  目的包大小 然后再重新运行抓包工具和 ...

  10. matlab中数据类型

    在MATLAB中有15种基本数据类型,分别是8种整型数据.单精度浮点型.双精度浮点型.逻辑型.字符串型.单元数组.结构体类型和函数句柄.这15种基本数据类型具体如下. 有符号整数型:int8,int1 ...