【poj2774】 Long Long Message
http://poj.org/problem?id=2774 (题目链接)
题意
给出两个只包含小写字母的字符串,求出最长连续公共子串。
solution
第一次用后缀数组,感觉有点神。。。才发现原来sa[0]是没用的。。
将两个字符串合并为一个,并用分隔符隔开。之后跑后缀数组,求出height[],for一遍,找到在分隔符两侧的height值最大的便是答案。
UPD 2017.1.11:
我以前写的什么东西。。贴一个板子,还是习惯下标从1开始→_→
代码
// poj2774
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=200010;
int rank[maxn],height[maxn],sa[maxn];
char s[maxn],s1[maxn]; namespace Suffix {
int wa[maxn],wb[maxn],ww[maxn];
int cmp(int *r,int a,int b,int l) {
return r[a]==r[b] && r[a+l]==r[b+l]; //这样写不会造成数组越界
}
void da(char *r,int *sa,int n,int m) {
int i,j,p,*x=wa,*y=wb;
for (i=0;i<=m;i++) ww[i]=0; //将桶清空
for (i=1;i<=n;i++) ww[x[i]=r[i]]++; //x记录名次
for (i=1;i<=m;i++) ww[i]+=ww[i-1]; //前缀和
for (i=n;i>=1;i--) sa[ww[x[i]]--]=i; //从n for到1和从1 for到n都可以
for (p=1,j=1;p<n;j*=2,m=p) { //j代表长度;如果已经有n个排名不同的后缀则完成;排完序后m就等于p
for (p=0,i=n-j+1;i<=n;i++) y[++p]=i; //它们没有第二关键字
for (i=1;i<=n;i++) if (sa[i]>j) y[++p]=sa[i]-j; //第二关键字排序
for (i=0;i<=m;i++) ww[i]=0; //第一关键字排序
for (i=1;i<=n;i++) ww[x[y[i]]]++;
for (i=1;i<=m;i++) ww[i]+=ww[i-1];
for (i=n;i>=1;i--) sa[ww[x[y[i]]]--]=y[i]; //这里一定要从n for 到1
for (swap(x,y),x[sa[1]]=p=1,i=2;i<=n;i++) //将名次储存在x里面
x[sa[i]]=cmp(y,sa[i-1],sa[i],j) ? p : ++p;
}
}
void calheight(char *r,int *sa,int n) {
for (int i=1;i<=n;i++) rank[sa[i]]=i;
for (int k=0,i=1;i<=n;i++) {
if (k) k--;
int j=sa[rank[i]-1];
while (s[i+k]==s[j+k]) k++;
height[rank[i]]=k; //注意这里是rank[i]
}
}
} int main() {
scanf("%s%s",s+1,s1+1);
int n=strlen(s+1);
int n1=strlen(s1+1);
s[++n]='#';int l=n;
for (int i=1;i<=n1;i++) s[n+i]=s1[i];
n+=n1;
using namespace Suffix;
da(s,sa,n,200);
calheight(s,sa,n);
int ans=0;
for (int i=2;i<=n;i++)
if ((sa[i-1]<l && sa[i]>l) || (sa[i-1]>l && sa[i]<l))
ans=max(ans,height[i]);
printf("%d",ans);
return 0;
}
Solution
后缀自动机。
将一个串构成后缀自动机以后,另一个串在上面匹配就可以了,跳par的时候长度也要更新。
代码
// poj2774
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<ctime>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
int n;
char s[maxn]; namespace SAM {
int last,Dargen,sz;
int len[maxn<<1],ch[maxn<<1][26],par[maxn<<1];
void Extend(int c) {
int np=++sz,p=last;last=np;
len[np]=len[p]+1;
for (;p && !ch[p][c];p=par[p]) ch[p][c]=np;
if (!p) par[np]=Dargen;
else {
int q=ch[p][c];
if (len[q]==len[p]+1) par[np]=q;
else {
int nq=++sz;len[nq]=len[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
par[nq]=par[q];
par[np]=par[q]=nq;
for (;p && ch[p][c]==q;p=par[p]) ch[p][c]=nq;
}
}
}
void build() {
Dargen=sz=last=1;
for (int i=1;i<=n;i++) Extend(s[i]-'a');
}
int query(char *s) {
int ans=0,ll=0;
int l=strlen(s+1);
for (int p=Dargen,i=1;i<=l;i++) {
while (p>1 && !ch[p][s[i]-'a']) p=par[p],ll=len[p];
if (ch[p][s[i]-'a']) {
p=ch[p][s[i]-'a'];
ll++;ans=max(ans,ll);
}
}
return ans;
}
}
using namespace SAM; int main() {
scanf("%s",s+1);
n=strlen(s+1);
build();
scanf("%s",s+1);
printf("%d",query(s));
return 0;
}
【poj2774】 Long Long Message的更多相关文章
- 【POJ2774】Long Long Message(后缀数组)
[POJ2774]Long Long Message(后缀数组) 题面 Vjudge Description Little cat在Byterland的首都读物理专业.这些天他收到了一条悲伤地信息:他 ...
- 【POJ2774】Long Long Message (后缀数组)
Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A p ...
- 【POJ2774】Long Long Message(后缀数组求Height数组)
点此看题面 大致题意: 求两个字符串中最长公共子串的长度. 关于后缀数组 关于\(Height\)数组的概念以及如何用后缀数组求\(Height\)数组详见这篇博客:后缀数组入门(二)--Height ...
- 【POJ2774】Long Long Message (SA)
最长公共子串...两个字符串连在一起,中间放一个特殊字符隔开.求出height之后,枚举height,看两个后缀是不是分布于两段字符串..如果是,这个值就可以作为答案.取最大值即可. ; var c, ...
- 【poj2774】Long Long Message
用个分隔符将两个字符串连接起来,再用后缀数组求出height数组的值,找出一个height值最大并且i与i-1的sa值分别在两串字符中就好 #include<algorithm> #inc ...
- laravel 【error】MethodNotAllowedHttpException No message
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message 报错原因[原理]CSRF ...
- 【后缀数组】【poj2774】【 Long Long Message】
题意: 求两个串的最长连续子串. 我的想法: 枚举第二个串...在第一个串的后缀数组中二分查找. 复杂度NlogN.最坏情况N^2 题解: (3)height 数组:定义height[i]=suffi ...
- 【JAVASCRIPT】获取触发MESSAGE事件的源IFRAME
先让发送源获取焦点,然后获取焦点元素. window.addEventListener('message',function(msg){ //做一些事来判断是不是某个iframe发送的消息 msg.s ...
- 【Linux】snmp在message中报错: /etc/snmp/snmpd.conf: line 311: Error: ERROR: This output format has been de
Apr 17 17:36:17 localhost snmpd[2810]: /etc/snmp/snmpd.conf: line 311: Error: ERROR: This output for ...
随机推荐
- selmodel
selmodel不仅可以设置checkbox,还可以设置单元格选中模式,列选择模式,行选择模式
- Html5 Egret游戏开发 成语大挑战(六)游戏界面构建和设计
本篇将主要讲解游戏界面的构建和设计,会应用到egret.eui的自定义组件,可以很直观的构建一个游戏整体,这里我们仍然只需要使用EgretWing就可以达到目的,本篇可能是篇幅最少的一个,但是涉及自定 ...
- 转:如何在32位程序中突破地址空间4G的限制
//如何在32位程序中突破地址空间4G的限制 //首先要获得内存中锁定页的权限 #define _WIN32_WINNT 0x0501 //xp系统 #include <windows.h> ...
- 在iframe中获取本iframe DOM引用
window.frameElement 获取本iframe DOM window.frameElement.contentDocument.getElementById('id') 获取这个ifram ...
- [转]C#如何把文件夹压缩打包然后下载
public partial class _Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventAr ...
- QT 智能提示设置
qt5.0的智能提示设置 qt默认的是Ctrl+空格 但这个是切换输入法,用着也不习惯 修改的地方是 工具->选项->环境 键盘选项把CompleteThis修改成自己习惯的快捷键
- 也来山寨一版Flappy Bird (js版)
随着Flappy Bird的火爆,各种实现的版也不断出现,于是也手痒简单实现了一版. 其实本来只是想实现一下这只笨鸟的飞翔运动的,后来没忍住,就直接实现一个完整游戏了…… 因为这个游戏本身实现起来就没 ...
- 高端大气上档次Ergotron Neo-Flex+MBP Retina的组合~
- [POJ2404]Jogging Trails(中国旅行商问题)(一般图的匹配——状压DP)
题目:http://poj.org/problem?id=2404 题意:有个n(n<=15)的点和m条无向边,每条边都有自己的权值.现在你要从某个点出发,每条边可以经过多次但要保证每条边至少走 ...
- 同态加密-Homomorphic encryption
同态加密(Homomorphic encryption)是一种加密形式,它允许人们对密文进行特定的代数运算得到仍然是加密的结果,将其解密所得到的结果与对明文进行同样的运算结果一样.换言之,这项技术令人 ...