SPOJ 10570 LONGCS - Longest Common Substring
思路
和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成多组数据就有三倍经验了
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int maxlen[202000],suflink[202000],barrel[202000],trans[202000][26],Nodecnt,ranks[202000];
int New_state(int _maxlen,int *_trans,int _suflink){
++Nodecnt;
maxlen[Nodecnt]=_maxlen;
if(_trans)
for(int i=0;i<26;i++)
trans[Nodecnt][i]=_trans[i];
suflink[Nodecnt]=_suflink;
return Nodecnt;
}
int add_len(int u,int c){
int z=New_state(maxlen[u]+1,NULL,0);
while(u&&trans[u][c]==0){
trans[u][c]=z;
u=suflink[u];
}
if(!u){
suflink[z]=1;
return z;
}
int v=trans[u][c];
if(maxlen[v]==maxlen[u]+1){
suflink[z]=v;
return z;
}
int y=New_state(maxlen[u]+1,trans[v],suflink[v]);
suflink[v]=suflink[z]=y;
while(u&&trans[u][c]==v){
trans[u][c]=y;
u=suflink[u];
}
return z;
}
void c_sort(int n,int lim){
memset(barrel,0,sizeof(barrel));
for(int i=1;i<=n;i++)
barrel[maxlen[i]]++;
for(int i=1;i<=lim;i++)
barrel[i]+=barrel[i-1];
for(int i=1;i<=n;i++)
ranks[barrel[maxlen[i]]--]=i;
}
int Ans[202000],mx[202000];
char s[202000];
void init(void){
Nodecnt=1;
memset(maxlen,0,sizeof(maxlen));
memset(trans,0,sizeof(trans));
memset(suflink,0,sizeof(suflink));
memset(ranks,0,sizeof(ranks));
memset(Ans,0,sizeof(Ans));
memset(mx,0,sizeof(mx));
}
int main(){
// freopen("test.in","r",stdin);
int T;
scanf("%d",&T);
while(T--){
init();
int cnt=0,last=1,numx;
Nodecnt=1;
int len;
scanf("%d",&numx);
for(int i=1;i<=numx;i++){
scanf("%s",s+1);
++cnt;
len=strlen(s+1);
if(cnt==1){
for(int i=1;i<=len;i++)
last=add_len(last,s[i]-'a');
c_sort(Nodecnt,200010);
for(int i=1;i<=Nodecnt;i++)
Ans[i]=maxlen[i];
}
else{
memset(mx,0,sizeof(mx));
int nowp=1,lent=0;
for(int i=1;i<=len;i++){
if(trans[nowp][s[i]-'a']){
lent++;
nowp=trans[nowp][s[i]-'a'];
}
else{
while(nowp&&trans[nowp][s[i]-'a']==0)
nowp=suflink[nowp];
if(!nowp){
nowp=1;
lent=0;
continue;
}
else{
lent=maxlen[nowp]+1;
nowp=trans[nowp][s[i]-'a'];
}
}
mx[nowp]=max(mx[nowp],lent);
}
for(int i=Nodecnt;i>=1;i--){
int t=ranks[i];
mx[suflink[t]]=max(mx[suflink[t]],mx[t]);
}
for(int i=1;i<=Nodecnt;i++)
Ans[i]=min(Ans[i],mx[i]);
}
}
int ans=0;
for(int i=1;i<=Nodecnt;i++)
ans=max(Ans[i],ans);
printf("%d\n",ans);
}
return 0;
}
SPOJ 10570 LONGCS - Longest Common Substring的更多相关文章
- SPOJ 1811 LCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- 【刷题】SPOJ 1811 LCS - Longest Common Substring
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- 【刷题】SPOJ 1812 LCS2 - Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【spoj LCS2】 Longest Common Substring II
http://www.spoj.com/problems/LCS2/ (题目链接) 题意 求多个串的最长公共子串 Solution 对其中一个串构造后缀自动机,然后其它串在上面跑匹配.对于每个串都可以 ...
- SPOJ 1812 LCS2 - Longest Common Substring II
思路 后缀自动机求多串的最长公共子串 对第一个建出后缀自动机,其他的在SAM上匹配,更新到一个节点的匹配长度最大值即可,最后对所有最大值取min得到一个节点的答案,对所有节点答案求max即可 然后注意 ...
随机推荐
- VUE-006-通过路由 router.push 传递 params 参数(路由 name 识别,请求链接不显示)
在前端页面表单列表修改时,经常需要在页面切换的时候,传递需要修改的表单内容,通常可通过路由进行表单参数的传递. 首先,配置页面跳转路由.在 router/index.js 中配置相应的页面跳转路由,如 ...
- js导航栏单击事件背景颜色变换
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- pandas(一)
import numpy as py import pandas as pd Series对象 data= pd.Series([0.25,0.5,0.75,1.0]) 默认索引是数字 data= ...
- matlab工作空间数据导入simulink
使用的是其中一种方式: 第一步在工作命令区 ,写命令: 第二步:保证导入simulink区,及from worker设置: 其中注意设置你的采样时间, 第三步设置scop : 采样时承接数据线上 ...
- Android使用https与服务器交互的正确姿势
HTTPS 使用 SSL 在客户端和服务器之间进行加密通信,错误地使用 SSL ,将会导致其它人能够拦截网络上的应用数据. 使用一个包含公钥及与其匹配的私钥的证书配置服务器,作为 SSL 客户端与服务 ...
- jenkins centos slave起不来报错The SSH key presented by the remote host does not match the key saved in the Known Hosts file against this host. Connections to this host will be denied until the two keys mat
场景:我的centos-204是一台centos的机器,本来用https://www.cnblogs.com/zndxall/p/8297356.html 的centos slave方式搭建ok的,一 ...
- principal(括号匹配+多组查询)
题目传送门: 把所有括号相匹配的段直接预处理出来就行了 #include <bits/stdc++.h> using namespace std; #define ll long long ...
- UML作业第二次:类图中类的表示
1.关于类图的学习: 类图显示了系统的静态结构. 类:类图中的主要元素,用矩形表示.矩形的上层表示类名.中层表示属性.下层表示方法. 类之间的关系:关联.依赖.聚集.泛化和实现五种. 2.五种类间关系 ...
- centos7.2 Apache+PHP7.2+Mysql5.6环境搭建
yum安装PHP7.2 由于linux的yum源不存在php7.x,所以我们要更改yum源:rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-re ...
- 软件测试之Soot
详情请见:https://github.com/fogmisty/SoftwareTest