POJ2774 Long Long Message [后缀数组]
Time Limit: 4000MS | Memory Limit: 131072K | |
Total Submissions: 29277 | Accepted: 11887 | |
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
Source
最长公共子串
字符串的任何一个子串都是这个字符串的某个后缀的前缀。
求 A 和 B 的最长公共子串等价于求 A 的后缀和 B 的后缀的最长公共前缀的最大值。
两个字符串,考虑合并到一起,用一个没出现过的字符隔开
height中sa[i]和sa[i-1]不在同一个串里的最大值就是答案
这个值一定存在,因为排序后中两个串都有,至少存在一个分界一边是A一边是B
- //
- // main.cpp
- // poj274
- //
- // Created by Candy on 2016/12/27.
- // Copyright © 2016年 Candy. All rights reserved.
- //
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- const int N=2e5+;
- int n,n1,n2,m,a[N];
- char s[N];
- int sa[N],c[N],t1[N],t2[N];
- inline bool cmp(int *r,int a,int b,int j){
- return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];
- }
- int rnk[N],height[N];
- void getHeight(int s[]){
- int k=;
- for(int i=;i<=n;i++) rnk[sa[i]]=i;
- for(int i=;i<=n;i++){
- if(k) k--;
- if(rnk[i]==) continue;
- int j=sa[rnk[i]-];
- while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
- height[rnk[i]]=k;
- }
- }
- void getSA(int s[]){
- int *r=t1,*k=t2;
- for(int i=;i<=m;i++) c[i]=;
- for(int i=;i<=n;i++) c[r[i]=s[i]]++;
- for(int i=;i<=m;i++) c[i]+=c[i-];
- for(int i=n;i>=;i--) sa[c[r[i]]--]=i;
- for(int j=;j<=n;j<<=){
- int p=;
- for(int i=n-j+;i<=n;i++) k[++p]=i;
- for(int i=;i<=n;i++) if(sa[i]>j) k[++p]=sa[i]-j;
- for(int i=;i<=m;i++) c[i]=;
- for(int i=;i<=n;i++) c[r[k[i]]]++;
- for(int i=;i<=m;i++) c[i]+=c[i-];
- for(int i=n;i>=;i--) sa[c[r[k[i]]]--]=k[i];
- swap(r,k);p=;r[sa[]]=++p;
- for(int i=;i<=n;i++) r[sa[i]]=cmp(k,sa[i],sa[i-],j)?p:++p;
- if(p>=n) break;m=p;
- }
- }
- void solve(){
- int mx=;
- for(int i=;i<=n;i++){
- if((sa[i]>n1&&sa[i-]<n1)||(sa[i-]>n1&&sa[i]<n1)) mx=max(mx,height[i]);
- }
- printf("%d",mx);
- }
- int main(){
- scanf("%s",s+);
- n1=strlen(s+);
- for(int i=;i<=n1;i++) a[i]=s[i];
- a[++n1]=;
- scanf("%s",s+);
- n2=strlen(s+);
- for(int i=;i<=n2;i++) a[i+n1]=s[i];
- n=n1+n2; //printf("%d %d %d\n",n1,n2,n);
- m=;
- getSA(a);
- getHeight(a);
- solve();
- return ;
- }
POJ2774 Long Long Message [后缀数组]的更多相关文章
- POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串
题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072 ...
- poj2774 Long Long Message 后缀数组求最长公共子串
题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...
- poj2774 Long Long Message(后缀数组or后缀自动机)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Long Long Message Time Limit: 4000MS Me ...
- (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 ...
- poj 2774 Long Long Message 后缀数组基础题
Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 24756 Accepted: 10130 Case Time Limi ...
- POJ2774Long Long Message (后缀数组&后缀自动机)
问题: The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to ...
- poj 2774 Long Long Message 后缀数组LCP理解
题目链接 题意:给两个长度不超过1e5的字符串,问两个字符串的连续公共子串最大长度为多少? 思路:两个字符串连接之后直接后缀数组+LCP,在height中找出max同时满足一左一右即可: #inclu ...
- POJ-2774-Long Long Message(后缀数组-最长公共子串)
题意: 给定两个字符串 A 和 B,求最长公共子串. 分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长公共子串等价于求 A 的后缀和 B 的后缀的最长公共前缀的最大 ...
随机推荐
- .NET平台开源项目速览(17)FluentConsole让你的控制台酷起来
从该系列的第一篇文章 .NET平台开源项目速览(1)SharpConfig配置文件读写组件 开始,不知不觉已经到第17篇了.每一次我们都是介绍一个小巧甚至微不足道的.NET平台的开源软件,或者学习,或 ...
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- Mac OS、Ubuntu 安装及使用 Consul
Consul 概念(摘录): Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,比如 Airbnb 的 SmartStac ...
- C#委托异步调用
参考页面: http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-get.html http://www.yuanjiaocheng.net/w ...
- arcgis api for js入门开发系列七图层控制(含源代码)
上一篇实现了demo的地图分屏对比模块,本篇新增图层控制模块,截图如下(源代码见文章底部): 图层控制模块实现的思路如下: 1.在地图配置文件map.config.js里面配置图层目录树节点信息,作为 ...
- 微信开发笔记(accesstoken)
access_token分两种 一种是公众号权限获取用,调用cgi-bin接口 ,此种token一个公众号同时只有一个,用这一个就够了. 服务器最好缓存. 用这个token前提是用户关注了此公众号. ...
- github免输用户名/密码SSH登录的配置
从github上获取的,自己整理了下,以备后用. Generating an SSH key mac windows SSH keys are a way to identify trusted co ...
- [原创]ubuntu16.04LTS使用细节
如何给自己安装的应用创建桌面图标 拿php开发神器phpstorm为例,找到可执行文件所在路径. 这里是/home/haive/PhpStorm/bin/phpstorm.sh 打开dash,搜索&q ...
- 按照Enterprise Integration Pattern搭建服务系统
在前一篇文章中,我们已经对Enterprise Integration Pattern中所包含的各个组成进行了简单地介绍.限于篇幅(20页Word以内),我并没有深入地讨论各个组成.但是如果要真正地按 ...
- Joshua Bloch错了? ——适当改变你的Builder模式实现
注:这一系列都是小品文.它们偏重的并不是如何实现模式,而是一系列在模式实现,使用等众多方面绝对值得思考的问题.如果您仅仅希望知道一个模式该如何实现,那么整个系列都会让您失望.如果您希望更深入地了解各个 ...