POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)
Time Limit: 4000MS | Memory Limit: 131072K | |
Total Submissions: 23696 | Accepted: 9705 | |
Case Time Limit: 1000MS |
Description
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 :(
Input
Output
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
Sample Output
27
Source
field=source&key=POJ+Monthly--2006.03.26">POJ Monthly--2006.03.26
ac代码
/*Problem: 2774 User: kxh1995
Memory: 5024K Time: 891MS
Language: C++ Result: Accepted */
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define min(a,b) (a>b? b:a)
using namespace std;
char str1[200100],str2[2000100];
int sa[220020],c[220020],t2[220020];
int t1[220020],s[200100];
int rank[220020],height[220020];
int len1,len2;
void build_sa(int s[],int n,int m)
{
int i,j,p,*x=t1,*y=t2;
for(i=0;i<m;i++)
c[i]=0;
for(i=0;i<n;i++)
c[x[i]=s[i]]++;
for(i=1;i<m;i++)
c[i]+=c[i-1];
for(i=n-1;i>=0;i--)
sa[--c[x[i]]]=i;
for(j=1;j<=n;j<<=1)
{
p=0;
for(i=n-j;i<n;i++)
y[p++]=i;
for(i=0;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
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];
swap(x,y);
p=1;
x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n)
break;
m=p;
}
}
void getHeight(int s[],int n)
{
int i,j,k=0;
for(i=0;i<=n;i++)
rank[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k)
k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k])
k++;
height[rank[i]]=k;
}
}
int judge(int len,int k)
{
int i;
for(i=1;i<len;i++)
{
if(height[i]>=k)
{
if(sa[i]>len1&&sa[i-1]<=len1)
return 1;
if(sa[i-1]>len1&&sa[i]<=len1)
return 1;
}
}
return 0;
}
int main()
{
while(scanf("%s%s",str1,str2)!=EOF)
{
int i;
len1=strlen(str1);
len2=strlen(str2);
for(i=0;i<len1;i++)
{
s[i]=str1[i]-'a'+1;
}
s[len1]=27;
int n=len1+1;
for(i=0;i<len2;i++)
s[n++]=str2[i]-'a'+1;
s[n]=0;
build_sa(s,len1+len2+3,28);
getHeight(s,len1+len2+2);
int l=0,r=min(len1,len2),ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(judge(len1+len2+2,mid))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",ans);
}
}
POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)的更多相关文章
- poj2774 Long Long Message 后缀数组求最长公共子串
题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...
- Long Long Message (poj2774 后缀数组求最长公共子串)
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 19206 Accepted: 79 ...
- poj2774 后缀数组 求最长公共子串
Reference:IOI2009论文 http://www.cnblogs.com/ziyi--caolu/p/3192731.html #include "stdio.h" # ...
- poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14874 Accepted: 5118 De ...
- HDU 1403 Longest Common Substring(后缀数组,最长公共子串)
hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- POJ 2774 Long Long Message (后缀数组+二分)
题目大意:求两个字符串的最长公共子串长度 把两个串接在一起,中间放一个#,然后求出height 接下来还是老套路,二分出一个答案ans,然后去验证,如果有连续几个位置的h[i]>=ans,且存在 ...
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...
- POJ 2774 Long Long Message 后缀数组
Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A ...
随机推荐
- Android error--No implementation found for native Lcomd
在利用NDK编译Cpp执行时,出现了No implementation found for native Lcom等错误,调试好久,才发现 XXX.h和XXX.cpp.在XXX.cpp里#includ ...
- ClassNotFoundException和NoClassDefFoundError的差别
正如它们的名字所说明的:NoClassDefFoundError是一个错误(Error),而ClassNOtFoundException是一个异常,在Java中错误和异常是有差别的,我们能够从异常中恢 ...
- 移除apsx视图引擎,及View目录下的web.config的作用
<> 使用Rezor视图引擎的时候移除apsx视图引擎 Global.asax文件 using System; using System.Collections.Generic; usin ...
- 7.gcc的使用
什么是gcc gcc编译器(GNU C Compiler) 现在我们所说的 gcc 是 GUN Compiler Collection的缩写,可以支持多种语言编译,比如 C,C++,Java, pas ...
- C++中的pair,make_pair学习
std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型.例如std::pair<int,float> 或者 std::pair<double,do ...
- php.ini控制文件上传大小配置项
; Whether to allow HTTP file uploads.file_uploads = On ; Temporary directory for HTTP uploaded files ...
- 获得IP地址中文
string ipFilePath = @"~/App_Data/QQWry.dat"; QQWryLocator QQWry = new QQWryLocator(Server. ...
- 微信小程序和App的UI设计有什么异同吗?
大家总是把小程序和App放在一起比,因此我也花时间看了一下小程序的开发指南,尤其是UI部分的设计和原则,今天就拿它和苹果的HIG(Human Interface Guidelines)做个比较,其实两 ...
- 关于Android Studio更新后一直Refreshing的解决办法!
今天更新了一下studio一直是这个问题 查了很多资料终于解决了 造成这个问题的原因是要更新的gradle版本和studio安装路径中的gradle版本不一致导致的 把他们改成一致的即可 在这个目录里 ...
- Thingworx SDK开发自定义Widget
Thingworx自带的图表数量有限,样式也很有限,在echarts上看到了这样一个非常简单的图表,下面将做一个简单的静态引入示范 首先创建Thingworx项目 然后右键ui新建widget 自动生 ...