思路

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的更多相关文章

  1. SPOJ 1811 LCS - Longest Common Substring

    思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...

  2. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  3. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  4. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

  5. 【刷题】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 ...

  6. 【刷题】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 ...

  7. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  8. 【spoj LCS2】 Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ (题目链接) 题意 求多个串的最长公共子串 Solution 对其中一个串构造后缀自动机,然后其它串在上面跑匹配.对于每个串都可以 ...

  9. SPOJ 1812 LCS2 - Longest Common Substring II

    思路 后缀自动机求多串的最长公共子串 对第一个建出后缀自动机,其他的在SAM上匹配,更新到一个节点的匹配长度最大值即可,最后对所有最大值取min得到一个节点的答案,对所有节点答案求max即可 然后注意 ...

随机推荐

  1. 在VM虚拟机中安装Centos操作系统

    首先我们要下载  Centos https://www.centos.org/ 这个是Centos官方 最新版本 7 https://www.centos.org/download/ 提供有 DVD安 ...

  2. LAYUI select 下拉框得高度

    页面下得select 框 在css页面加样式 .layui-form-select dl {    max-height: 152px;}

  3. 爬虫下载校花网美女信息-lxml

    # coding=utf-8 # !/usr/bin/env python ''' author: dangxusheng desc : 下载校花网上的个人信息:名字-学校-图片地址-点赞数 date ...

  4. ADB——应用交互

    使用ADB与手机应用交互 应用交互的操作包括:启动应用 / 调起Activity.调起Services.停止Service.发送广播.强行停止应用 基本命令 am <command> 常用 ...

  5. vue中keep-alive使用时,注意要点

    <keep-alive exclude="QRCode,NewsInfor,VipRecordDetail"> <router-view></rout ...

  6. python 模拟实现一个ATM + 购物商城程序

    思路:ATM是一个单独程序,提供给消费的是一个接口core下的settlement.py,只做了个人的,没写管理的模块 Shopping也是一个单独的,只做了一个购物的消费模块,没写商家模块,偷懒用了 ...

  7. 搭建apache本地服务器·Mac

    1. 打开终端,开启Apache: //开启apache: sudo apachectl start //重启apache: sudo apachectl restart //关闭apache: su ...

  8. ultiple Endpoints may not be deployed to the same path

    @Configurationpublic class WebSocketConfig { //打war包启动需要注释掉此:否则报 :DeploymentException: Multiple Endp ...

  9. tree状数据叶子节点与根节点等的递归转换

    做项目时经常遇到树状层级数据.从各个层级数据的转换查询等.场景如行业类别的多层级,行政区层级,检查项类别层级等等. 数据结构如  Id Name ParentId #region area树状节点的转 ...

  10. JavaScript中创建对象的几种模式

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...