POJ2274(后缀数组应用)
Time Limit: 4000MS | Memory Limit: 131072K | |
Total Submissions: 25272 | Accepted: 10345 | |
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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=;
char buf[MAXN];
int sa[MAXN];
int rnk[MAXN];
int tmp[MAXN];
int lcp[MAXN];
int len,k; bool comp(int i,int j)
{
if(rnk[i]!=rnk[j]) return rnk[i]<rnk[j];
else{
int ri=(i+k<=len)?rnk[i+k]:-;
int rj=(j+k<=len)?rnk[j+k]:-;
return ri<rj;
}
} void getsa()
{
memset(rnk,,sizeof(rnk));
memset(sa,,sizeof(sa));
memset(tmp,,sizeof(tmp)); len=strlen(buf);
for(int i=;i<len;i++)
{
sa[i]=i;
rnk[i]=buf[i]-'a';
}
sa[len]=len;
rnk[len]=-; for(k=;k<=len;k*=)
{
sort(sa,sa+len+,comp); tmp[sa[]]=;
for(int i=;i<=len;i++)
{
tmp[sa[i]]=tmp[sa[i-]]+(comp(sa[i-],sa[i])?:);
} for(int i=;i<=len;i++)
{
rnk[i]=tmp[i];
}
}
} void getlcp()
{
memset(rnk,,sizeof(rnk));
memset(lcp,,sizeof(lcp));
getsa();
for(int i=;i<=len;i++)
{
rnk[sa[i]]=i;
} int h=;
lcp[]=h;
for(int i=;i<len;i++)
{
int j=sa[rnk[i]-];
if(h>) h--;
for(;h+i<len&&h+j<len;h++)
if(buf[i+h]!=buf[j+h]) break;
lcp[rnk[i]-]=h;
}
} void debug()
{
for(int i=;i<=len;i++)
{
int l=sa[i];
if(l==len)
{
printf("%d %d\n",l,lcp[i]);
}
else
{
for(int j=l;j<len;j++)
printf("%c ",buf[j]);
printf("%d\n",lcp[i]);
}
} }
char ss[MAXN];
int main()
{
while(scanf("%s",buf)!=EOF)
{
int l=strlen(buf);
buf[l]='$';
scanf("%s",ss);
strcat(buf,ss);
getlcp();
//debug();
int ans=;
for(int i=;i<len;i++)
{
if((sa[i]<l)!=(sa[i+]<l))
ans=max(ans,lcp[i]);
}
printf("%d\n",ans);
} return ;
}
POJ2274(后缀数组应用)的更多相关文章
- 后缀数组的倍增算法(Prefix Doubling)
后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- 后缀数组(suffix array)详解
写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...
- 【UOJ #35】后缀排序 后缀数组模板
http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...
- 【BZOJ-2119】股市的预测 后缀数组
2119: 股市的预测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 334 Solved: 154[Submit][Status][Discuss ...
- 【BZOJ-4698】Sandy的卡片 后缀数组
4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 140 Solved: 55[Submit][Stat ...
随机推荐
- 杂谈之WEB前端project师身价
了解javascript语言规范么?+1000 知道各浏览器的css差异么?+1000 javascript差异呢?+1000 知道html各标签的含义并不是常好地运用么?+1000 知道怎样跨浏览器 ...
- POJ2386 Lake Counting 【DFS】
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20782 Accepted: 10473 D ...
- IOS_DatePicker_PickerView_SegmentControl_键盘处理
H:/0712/01_UIController_MJViewController.m // MJViewController.m // 01-总结复习 // Created by apple on 1 ...
- Odoo10尝鲜: 采购协议
Odoo10 对 call for bid 进行了 改进, 作为 '采购协议' 进入 采购, 选择 'Purchase agreement' 在 agreement 列表 建立新的 协议 选择 协议类 ...
- Foundation框架 - NSException类
NSException类 WBStudentManager.h #import <Foundation/Foundation.h> NSString* const NameInvalidE ...
- 每天一个linux命令(23):Linux 目录结构(转)
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...
- kubernetes调度之 PriorityClass
系列目录 kubernetes支持多种资源调度模式,前面讲过简单的基于nodeName和nodeSelector的服务器资源调度,我们称之为用户绑定策略,下面简要描述基于PriorityClass的同 ...
- css3背景及字体渐变
1.背景渐变: .linear { width: 100%; FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,sta ...
- C# WPF DataGrid控件实现三级联动
利用DataGrid控件实现联动的功能,在数据库客户软件中是随处可见的,然而网上的资料却是少之又少,令人崩溃. 本篇博文将介绍利用DataGrid控件模板定义的三个ComboBox实现“省.市.区”的 ...
- 说说设计模式~单件模式(Singleton)
单件模式(Singleton)要求一个类有且仅有一个实例,并且提供了一个全局的访问点. 从概念上来研究一下它的实现,不考虑线程安全 1 public sealed class Singlton 2 { ...