Virtual Judge SPOJ - LCS2 Longest Common Substring II
https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的。
注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理!所以说不定哪一天就wa了!为什么我不改呢?(因为我懒)
多个串的最长公共子串,每次匹配某个状态找到的最大值取最小值,最后再把所有状态的值取一个最大值就可以了,因为涉及到在状态上值的储存所以要按照len从大到小排序。
这次ac自动机写的很棒没有出错但是
这个地方之前写错了,果然还是太蠢了。。记一下吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
const int maxn=;
char ch1[maxn]={},ch2[maxn]={};
int siz1,siz2;
struct nod{
int sig[];
int f,len;
}t[maxn*];int tot=,la=;
int cnt[maxn*]={};
int b[maxn*]={};
int now[maxn*]={};
int mx[maxn*]={};
void add(int z){
int x=++tot;int i=la;
t[x].len=t[la].len+;
for(;i&&!t[i].sig[z];i=t[i].f)
t[i].sig[z]=x;
if(!i)t[x].f=;
else{
int p=t[i].sig[z];
if(t[p].len==t[i].len+)t[x].f=p;
else{
int y=++tot;
t[y]=t[p];t[y].len=t[i].len+;
t[x].f=t[p].f=y;
for(;i&&t[i].sig[z]==p;i=t[i].f){
t[i].sig[z]=y;
}
}
}
la=x;
}
void pai(){
for(int i=;i<=tot;i++)cnt[t[i].len]++;
for(int i=;i<=siz1;i++)if(i)cnt[i]+=cnt[i-];
for(int i=tot;i;i--)b[cnt[t[i].len]--]=i;
}
int main(){
//freopen("a.in","r",stdin);
memset(t,,sizeof(t));
scanf("%s",ch1+);siz1=strlen(ch1+);
for(int i=;i<=siz1;i++)add(int(ch1[i]-'a'));
pai();
memset(mx,,sizeof(mx));
while(~scanf("%s",ch2+)){
siz2=strlen(ch2+);
int j=,tmp=;
memset(now,,sizeof(now));
for(int i=;i<=siz2;i++){
int z=ch2[i]-'a';
if(t[j].sig[z]){j=t[j].sig[z];tmp++;}
else{
while(j&&!t[j].sig[z])
j=t[j].f;
if(!j){j=;tmp=;}
else {tmp=t[j].len+;j=t[j].sig[z];}
}
if(now[j]<tmp)now[j]=tmp;
}
for(int i=tot;i>=;i--){
int z=b[i];
mx[z]=min(mx[z],now[z]);
if(now[z]&&t[z].f)now[t[z].f]=t[t[z].f].len;
}
}
int ans=;
for(int i=;i<=tot;i++)ans=max(mx[i],ans);
printf("%d",ans);
return ;
}
Virtual Judge SPOJ - LCS2 Longest Common Substring II的更多相关文章
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】
多串LCS很适合SA但是我要学SAM 对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c ...
- SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解
题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
随机推荐
- 【CodeForces】960 F. Pathwalks 主席树+动态规划
[题目]F. Pathwalks [题意]给定n个点m条边的有向图,可能不连通有重边有自环.每条边有编号 i 和边权 wi ,求最长的路径(可以经过重复节点)满足编号和边权都严格递增.n,m,wi&l ...
- 关于HttpWebRequest发生服务器协议冲突的解决办法
WinForm下的app.config文件中添加: <system.net> <settings> <httpWebRequest useUnsafeHe ...
- 【CF343D】 Water Tree(树链剖分)
题目链接 树剖傻逼题,练练手好久没写树剖了. 查询忘记\(pushdown\)抓了好久虫.. 全文手写,一遍过... #include <cstdio> const int MAXN = ...
- redis笔记之两种持久化备份方式(RDB & AOF)
Redis支持的两种持久化备份方式(RDB & AOF) redis支持两种持久化方式,一种是RDB,一种是AOF. RDB是根据指定的规则定时将内存中的数据备份到硬盘上,AOF是在每次执行命 ...
- Verilog笔记.5.同步、异步
在数字电路中经常有同步synchronism.异步asynchronism的概念.异步指输入信号和时钟无关:同步指输入信号和时钟信号有关,实际上就是输入信号和时钟信号进行了与运算或者与非运算.实际开发 ...
- 深入理解Spring系列之八:常用的扩展接口
转载 https://mp.weixin.qq.com/s/XfhZltSlTall8wKwV_7fKg Spring不仅提供了一个进行快速开发的基础框架,而且还提供了很多可扩展的接口,用于满足一些额 ...
- perl6 单线程破解phpmyadmin脚本
use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; my $url = 'http://localhost/phpMyAdmin/index.php' ...
- windows安装linux虚拟机、修改apt源
记录一下windows安装虚拟机以及初始配置的一些坑. 安装VMware Workstation 直接百度搜索VMware,选择合适的版本下载: 按照一般软件的安装步骤安装VMware Worksta ...
- MGR_ERROR 3092 (HY000): DROP DATABASE failed;
start group_replication;时报以下错: ERROR 3092 (HY000): DROP DATABASE failed; some tables may have been d ...
- WDCP各种停止重启命令
service wdapache start|stop|restart wdcp后台 启动|停止|重起service nginxd start|stop|restart nginx ...