Long Long Message 后缀数组入门题
| Time Limit: 4000MS | Memory Limit: 131072K | |
| Total Submissions: 22564 | Accepted: 9255 | |
| Case Time Limit: 1000MS | ||
Description
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 :(
Input
Output
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
Sample Output
27 给你两串字符,要你找出在这两串字符中都出现过的最长子串
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
// KMP TRIE DFA SUFFIX
int dp[MS][]; // RMQ
int t1[MS],t2[MS],c[MS],v[MS];
int rank[MS],sa[MS],height[MS];
char str[MS],str1[MS];
int s[MS];
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
} void get_sa(int *r,int *sa,int n,int m)
{
int i,j,p,*x=t1,*y=t2;
for(i=;i<m;i++)
c[i]=;
for(i=;i<n;i++)
c[x[i]=r[i]]++;
for(i=;i<m;i++)
c[i]+=c[i-];
for(i=n-;i>=;i--)
sa[--c[x[i]]]=i;
p=;j=;
for(;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++)
y[p++]=i;
for(i=;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=;i<n;i++)
v[i]=x[y[i]];
for(i=;i<m;i++)
c[i]=;
for(i=;i<n;i++)
c[v[i]]++;
for(i=;i<m;i++)
c[i]+=c[i-];
for(i=n-;i>=;i--)
sa[--c[v[i]]]=y[i];
swap(x,y);
x[sa[]]=;
for(p=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
} void get_height(int *r,int n)
{
int i,j,k=;
for(i=;i<=n;i++)
rank[sa[i]]=i;
//height[i]>=height[i-1]-1;
for(i=;i<n;i++)
{
if(k)
k--;
else
k=;
j=sa[rank[i]-];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
} int main()
{
scanf("%s%s",str,str1);
int n=,len=strlen(str);
for(int i=;i<len;i++)
s[n++]=str[i]-'a'+;
s[n++]=; // 分隔符
len=strlen(str1);
for(int i=;i<len;i++)
s[n++]=str1[i]-'a'+;
s[n]=; // 为了方便比较
get_sa(s,sa,n+,);
get_height(s,n);
int maxv=;
len=strlen(str);
for(int i=;i<n;i++)
{
if(height[i]>maxv)
{
if(<=sa[i-]&&sa[i-]<len&&len<sa[i])
maxv=height[i];
if(<=sa[i]&&sa[i]<len&&len<sa[i-])
maxv=height[i];
}
}
printf("%d\n",maxv);
return ;
}
Long Long Message 后缀数组入门题的更多相关文章
- POJ 2774 Long Long Message 后缀数组模板题
题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #i ...
- poj 2774 Long Long Message 后缀数组基础题
Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 24756 Accepted: 10130 Case Time Limi ...
- BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题
BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题 将字符串复制一遍接在原串后面,然后后缀排序即可. #include <cmath> #include &l ...
- 后缀数组入门(二)——Height数组与LCP
前言 看这篇博客前,先去了解一下后缀数组的基本操作吧:后缀数组入门(一)--后缀排序. 这篇博客的内容,主要建立于后缀排序的基础之上,进一步研究一个\(Height\)数组以及如何求\(LCP\). ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- POJ - 2774 Long Long Message (后缀数组/后缀自动机模板题)
后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> ...
- (HDU 5558) 2015ACM/ICPC亚洲区合肥站---Alice's Classified Message(后缀数组)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5558 Problem Description Alice wants to send a classi ...
- POJ 2774 Long Long Message 后缀数组
Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A ...
- POJ2774 & 后缀数组模板题
题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...
随机推荐
- Hadoop MapReduce概念学习系列之map并发任务数和reduce并发任务数的原理和代码实现(十八)
首先,来说的是,reduce并发任务数,默认是1. 即,在jps后,出现一个yarnchild.之后又消失. 这里,我控制reduce并发任务数6 有多少个reduce的并发任务数可以控制,但有多少个 ...
- android学习笔记(入门篇)
+号只是当你第一次定义一个资源ID的时候需要, 告诉SDK此资源ID需要被创建出来 对于所有的View默认的权重是0,如果你只设置了一个View的权重大于0,那么这个View将占据除去别的View本身 ...
- vimdiff
[vimdiff] 启动方法 首先保证系统中的diff命令是可用的.Vim的diff模式是依赖于diff命令的.Vimdiff的基本用法就是: # vimdiff FILE_LEFT FILE_RIG ...
- GetQueuedCompletionStatus的返回值
完成端口GetQueuedCompletionStatus返回值的问题 先看看GetQueuedCompletionStatus函数的完整声明:BOOL GetQueuedCompletionStat ...
- C#用串口接收事件接不全数据的处理
问题描述:都知道用事件dataReceive来处理串口非常的方便,但当一次的数据过长时,就会出现截断数据的情况.比如说发一个指 令,返回一个30个字节的数据,但上位机则分两次来接收者30个数据. 解决 ...
- My集合框架第二弹 二叉树的实现
package com.wpr.collection; import java.util.NoSuchElementException; public class BinarySearchTree&l ...
- 那些不被关注但很重要的html标签
1.meta标签: <meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部, ...
- GotGitHub
github在线教程 http://www.worldhello.net/gotgithub/
- Js弹出层,弹出框代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php 扩展 redis
1.通过phpinfo 查看php的版本( 要注意php 是nts 还是ts 通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabl ...