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

  1. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  2. 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 ...

  3. SPOJ LCS2 - Longest Common Substring II 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...

  4. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

  5. 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 ...

  6. SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解

    题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...

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

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

  8. spoj1812 LCS2 - Longest Common Substring II

    地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags  A string is fi ...

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

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

随机推荐

  1. JavaScript事件和方法

    单击一个超链接触发事件 1.用a标签的onclick <a href="#" onclick="js代码"> 这种写法呢,存在一种弊端,就是点击后会 ...

  2. form表单token错误

    添加一段代码试试: <input type="hidden" name="_token" value="{{ csrf_token() }}&q ...

  3. 信息收集之zoomeye

    一.浏览器上使用api接口 1.https://api.zoomeye.org/user/login post传参:{"username" : "username&quo ...

  4. 天梯赛 L2-014 列车调度 (模拟)

    火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口 ...

  5. 遍历目录大小——php经典实例

    遍历目录大小——php经典实例 <?php function dirSize($dir){ //定义大小初始值 $sum=; //打开 $dd=opendir($dir); //遍历 while ...

  6. java在CMD窗口执行程序的时候输入密码(隐藏一些敏感信息)

    有时候我们需要从CMD窗口执行一些命令,有时候会输入一些敏感的信息,比如密码之类的东西,所以我们可以从控制台读取但是不希望别人看见我们的密码: import java.io.Console; /** ...

  7. .NET Framework 4安装失败

    #刚装系统遇到之前所遇到的问题.之前因为这个事情还被困扰了好一阵子.特此写出来分享给大家. 环境:WIN10 企业版 在使用一些需要较高.net版本的时候无法更新.你可以试一下.在服务里面开启再更新. ...

  8. 宋牧春: Linux设备树文件结构与解析深度分析(1) 【转】

    转自:https://mp.weixin.qq.com/s/OX-aXd5MYlE_YoZ3p32qWA 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...

  9. 安全控制 iptables

    转自:http://www.opsers.org/linux-home/videos/chapter-netfilter-iptables-raiders.html 内容简介防火墙的概述iptable ...

  10. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:

    使用自己的jdk