【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 ...
随机推荐
- PAT 1018. 锤子剪刀布 (20)
现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行给出正整数N(<=105),即双方交锋的次数.随后N行,每行给出一次交锋的信息,即 ...
- PAT 1017. A除以B (20)
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- mongodb分布式查询
分布式查询:mongodb的分布式模型分为replica set和sharded cluster. sharded集群中将read根据sharding key(分片键)转发到指定的shard节点,re ...
- checkbox页面全选
http://pan.baidu.com/s/1tfzSa
- FFmpeg 1.2 for Android 生成一个动态库
上一篇<FFmpeg 1.2 for Android 编译动态库>里沃特跟大家介绍了如何编译动态库,但当时所生成的动态库总共包含10个so文件,这样要是加载起来会严重影响软件的启动速度,后 ...
- IIS W3SVC 无法启动1068错误的解决
苦苦寻找解决方法多天之后,终于看到了最简单的处理方法. 故障: 试遍网上各种方法,司马当活马,CMD下输入如下命令,然后重启: fsutil resource setautoreset true C: ...
- 基于FPGA的电压表与串口通信(上)
实验原理 该实验主要为利用TLC549采集模拟信号,然后将模拟信号的数字量通过串口发送到PC上上位机进行显示,使用到的TLC549驱动模块在进阶实验已经使用到了,串口模块在基础实验也已经使用到了,本实 ...
- 【BZOJ1002】【FJOI2007】轮状病毒(生成树计数)
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1766 Solved: 946[Submit][Status ...
- php 实现接收客户端上传的图片
今天,遇到一个服务端接收客户端上传图片的需求,经过学习.我写了个简单的demo 以备下次学习. 首先服务器接收的发送图片的请求一定要是post请求,而且请求一定要加上 enctype="mu ...
- Ceph常用维护操作
查看ceph 集群状态 1.ssh 登陆任一MON主机 2.执行 sudo ceph health detail 命令 启动.停止.重启.查看MON进程 1.登陆到MON的服务器,执行如下命令 sud ...