[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;
}

最新文章

  1. 走进云背后:微软Azure web 项目通过web service部署web site
  2. Windows 保存BMP图片
  3. mongo(三)基本操作
  4. 内存管理和@property的属性
  5. Java多线程——&lt;三&gt;简单的线程执行:Executor
  6. C#的WebBrowser操作frame如此简单
  7. How to install VXDIAG Honda, Toyota and JLR SDD software
  8. android 控件花屏问题
  9. HTTP缓存机制梳理
  10. AndroidMainifest标签说明2——&amp;lt;activity&amp;gt;
  11. nodejs 初次链接 mongodb 的详细细节
  12. 项目详解4—haproxy 详解
  13. JavaWeb之数据源连接池(1)---DBCP
  14. Codeforces D. Sorting the Coins
  15. 三十六、fetch
  16. 【ORA-12516 TNS监听程序找不到符合协议堆栈要求的可用处理程序】
  17. 深入学习 Java 序列化
  18. win server 2008添加磁盘-脱机转换为联机状态方法
  19. 通过adb命令在Android设备中执行Java命令, 并调用so文件。
  20. 《编程快速上手》--web抓取--利用webbrowser模块的mapIT.py

热门文章

  1. 暴力 ZOJ 1403 Safecracker
  2. 转 SQLPLUS中SQL换行执行
  3. [转]符号和运算符参考 (F#)
  4. Elasticsearch--集群&amp;时光之门和恢复控制
  5. 11 DOM基础
  6. scala打印error,debug,info
  7. YOLO模型对图片中车辆的识别比对
  8. OpenGL VAO, VBO 使用简介
  9. WEB下的excel批量导入功能
  10. swift 语言评价