spoj Longest Common Substring (多串求最大公共子序列)
题目链接:
https://vjudge.net/problem/SPOJ-LCS
题意:
最多10行字符串
求最大公共子序列
数据范围:
$1\leq |S| \leq100000$
分析:
让他们都和第一个字符串匹配,算出每个字符串与第一个字符串的,以$i$位置(i指的是在s1中的位置)结尾匹配的最大长度
与其它字符串的匹配取最小值
最后对所有位置取最大值
超时代码:(题限是236ms,这个代码跑2000ms没问题)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=500000*2+7;
char S[maxn];
char S1[maxn/2];
int ans[maxn/2];
int n,a,b,len1,len2;
struct suffixautomation
{
int mp[maxn*2][30],fa[maxn*2],ed,ct,len[maxn*2],minpos[maxn*2],maxpos[maxn*2],deg[maxn*2];
int res[maxn/2];
suffixautomation(){
for(int i=0;i<maxn*2;i++)minpos[i]=1e9,maxpos[i]=-1;
ed=ct=1;}
inline void ins(int c,int pos)
{
int p=ed;ed=++ct;minpos[ed]=maxpos[ed]=pos;len[ed]=pos;//先初始化size和len
for(;p&&mp[p][c]==0;p=fa[p]){mp[p][c]=ed;}//然后顺着parent树的路径向上找
if(p==0){fa[ed]=1;return;}int q=mp[p][c];//case1
if(len[p]+1==len[q]){fa[ed]=q;return;}//case2
len[++ct]=len[p]+1;//case 3
for(int i=1;i<=26;i++){mp[ct][i]=mp[q][i];}
fa[ct]=fa[q];fa[q]=ct;fa[ed]=ct;
for(int i=p;mp[i][c]==q;i=fa[i]){mp[i][c]=ct;}
}
void solve(){ for(int i=1;i<=ct;i++)deg[fa[i]]++;
queue<int>que;
for(int i=1;i<=ct;i++)if(deg[i]==0)que.push(i);
while(que.size()){
int x=que.front();
int y=fa[x];
que.pop();
minpos[y]=min(minpos[x],minpos[y]);
maxpos[y]=max(maxpos[x],maxpos[y]);
deg[y]--;
if(deg[y]==0)que.push(y);
}
for(int i=1;i<=ct;i++)
if(minpos[i]<=len1&&maxpos[i]>=len1+2)
res[minpos[i]]=max(res[minpos[i]],len[i]);
}
}sam,sam2;
int main()
{
// cout<<"acfvd"
for(int i=0;i<maxn/2;i++)ans[i]=1e9;
scanf("%s",S+1);
len1=strlen(S+1);
S[len1+1]='z'+1;
while(scanf("%s",S1+1)==1){
len2=strlen(S1+1);
sam=sam2;
for(int i=len1+2;i<=len1+len2+1;i++)
S[i]=S1[i-len1-1];
for(int i=1;i<=len1+len2+1;i++)sam.ins(S[i]-'a'+1,i);
sam.solve();
for(int i=1;i<=len1;i++)
ans[i]=min(ans[i],sam.res[i]);
}
int res=0;
for(int i=1;i<=len1;i++)
if(ans[i]!=1e9)res=max(res,ans[i]);
printf("%d\n",res);
return 0;
}
spoj Longest Common Substring (多串求最大公共子序列)的更多相关文章
- spoj - Longest Common Substring(后缀自动机模板题)
Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...
- spoj Longest Common Substring
Longest Common Substring SPOJ - LCS 题意:求两个串的最长公共子串 /* 对一个串建立后缀自动机,用另一个串上去匹配 */ #include<iostream& ...
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
- SPOJ Longest Common Substring II
题目连接:戳我 题目大意:求n个字符串的最长公共子串. 它的简化版--这里 当然我们可以用SA写qwq,也可以用广义SAM写qwq 这里介绍纯SAM的写法...就是对其中一个建立后缀自动机,然后剩下的 ...
- 2018.12.15 spoj Longest Common Substring II(后缀自动机)
传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...
- 【SPOJ】1812. Longest Common Substring II(后缀自动机)
http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
随机推荐
- 转 无损转换Image为Icon
不可取 var handle = bmp.GetHicon(); //得到图标句柄return Icon.FromHandle(handle); //通过句柄得到图标 可取 /// <su ...
- module 'cv2' has no attribute 'KNearest_create'
python版本:3.6.5 opencv版本:3.2.0 使用的是jupyter notebook 源代码如下: import cv2 import numpy as np import matpl ...
- Android NDK 学习之接受Java传入的Int数组
本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...
- apache ftp server 设置
<server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w ...
- OF框架使用指导系列索引
一.项目搭建指导 [OF框架]搭建标准工作环境 [OF框架]使用OF框架创建应用项目 [OF框架]在Visual Studio中启用Docker支持,编译生成,并在容器运行项目 [OF框架]在Visu ...
- 基于beautifulSoup进行电影网站排名的获取与格式化输出
要求 编写代码完成以下任务: ① 将地址"http://www.cbooo.cn/year?year=2019"源代码使用任意方法保存到指定文件中(文件类型不限). ② 使用文件流 ...
- spring--分类索引
1.过时api Spring(一)解决XmlBeanFactory过时问题 元素 'ref' 中不允许出现属性 'local' Spring学习笔记 关于spring 2.x中dependency-c ...
- 蓝桥杯 ALGO-156 表达式计算 JAVA代码 栈的应用
算法训练 表达式计算 时间限制:1.0s 内存限制:256.0MB 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个 ...
- Codeforces 348 D - Turtles Lindström–Gessel–Viennot lemma
#include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second ...
- MySQL服务器2
1.sql的基本语法 对数据库 create database db1; 创建数据库 对表: create database t1(id int,name char(10)); 创建表 show cr ...