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即可 然后注意 ...
随机推荐
- mysql8.0.13免安装版的安装配置详解
一.下载 下载地址:https://dev.mysql.com/downloads/mysql/ 二.解压到某个目录,例如:D:/mysql/mysql-8.0.13-winx64 三.配置环境变量 ...
- Centos7安装并配置mysql5.6
1.下载安装包:https://pan.baidu.com/s/18xAumOggjm9bu9Wty6kYjg 2.卸载系统自带的Mariadb 2.1查询已安装的mariadb [root@loca ...
- Mac上安装Charles进行抓包全流程设置
安装 -- 官网下载最新版的Charles版本,按照提示安装即可 破解 -- https://blog.csdn.net/qq_25821067/article/details/79848589. M ...
- 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的,一 ...
- 安装pwntools及对于解决问题方法搜索的经验总结
安装pwntools 按照网站(https://www.cnblogs.com/xiao3c/p/6799745.html) 中的教程进行安装 下载pwntools 输入命令 git clone ht ...
- ionic3 小记录
cordova platform add ios@latest 安装最新ios ionic cordova build ios -- --buildFlag="-UseModernBuild ...
- Python3.0科学计算学习之绘图(一)
基本绘图: (1) plot是标准的绘图库,调用函数plot(x,y)就可以创建一个带有绘图的图形窗口(其中y是x的函数).输入的参数为具有相同长度的数组(或列表):或者plot(y)是plot(r ...
- Pyenv部署
一.Git克隆方式 1.安装git yum -y install git 2.克隆pyenv到本地 git clone https://github.com/pyenv/pyenv.git ~/.py ...
- xpath详细讲解
什么是XML XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 的标签需要 ...
- AL32UTF8 and UTF8 and ZHS16GBK
About AL32UTF8 ORACLE数据库字符集,即Oracle全球化支持(Globalization Support), 或即国家语言支持(NLS)其作用是用本国语言和格式来存储.处理和检索数 ...