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即可 然后注意 ...
随机推荐
- Jmeter正则表达式提取器
https://www.cnblogs.com/tudou-22/p/9566894.html Jmeter中关联是通过“添加—后置处理器—正则表达式提取器”来获取数据 一.获取单个数据
- eclipse安装使用fat打jar包
在线安装步骤: eclipse菜单栏 help >software updates >Search for new features to install>new update si ...
- Redis的数据结构之sorted-set
存储Sorted-Set Sorted-Set和Set的区别 Sorted-Set中的成员在集合中的位置是有序的 存储Sorted-set常用命令 添加元素 获得元素 删除元素 范围查询 扩展命令 z ...
- Xamarin.Forms 自定义 TapGestureRecognizer 附加属性
While creating Xamarin.Forms applications, definitely you are going to need TapGestureRecognizer oft ...
- 面向对象:MATLAB的自定义类 [MATLAB]
https://www.cnblogs.com/gentle-min-601/p/9785812.html 面向对象:MATLAB的自定义类 [MATLAB] 这几天刚刚开始学习MATLAB的面向 ...
- php----------php安装xhprof扩展和简单使用
1.下载源码包 https://github.com/longxinH/xhprof (wget https://github.com/longxinH/xhprof/archive/master. ...
- eclipse 编码改成utf-8
Eclipse的编码格式是系统默认 修改为utf-8,点击Apply and Close 然后项目的编码格式会统一默认utf-8 当然也可以选择other,改成GBK.
- 接口自动化测试持续集成--Soapui接口功能测试参数化
按照自动化测试分层实现的原理,每一层的脚本实现都要进行参数化,自动化的目标就是要实现脚本代码与测试数据分离. 当测试数据进行调整的时候不会对脚本的实现带来震荡,从而提高脚本的稳定性与灵活度,降低脚本的 ...
- sqlserver 导入excel
安装完office2013和sqlserver2017时,不知道是盗版问题还是啥 原因, 在sqlserver导入excel时,报错. 在把Excel导入SQL server时出现“未在本地计算机上注 ...
- CentOS 6.9 升级OpenSSH版本 关闭ssh服务后门
最近用低版本的OpenSSH(5.9p1版本) 的漏洞给系统留了个后门 , 可以劫持root密码或者给root开启后门密码 : 利用Openssh后门 劫持root密码 如果公司还在用CentOS6的 ...