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 ...
随机推荐
- web前端如何优化自己的代码
前端的性能优化主要分为三部分: HTML优化 避免 HTML 中书写 CSS 代码,因为这样难以维护. 使用Viewport加速页面的渲染. 使用语义化标签,减少 CSS 代码,增加可读性和 SEO. ...
- vue中将时间戳转换为YYYY-MM-dd hh:mm格式时间的组件
首先我们可以使用vue中的过滤方法将数据变成另一个格式 // html <span class="rate-time">{{rating.rateTime | form ...
- ABAP-JCO服务端连接问题
公司网络服务加域,若SAP服务器后端未配置端口号映射,则外部服务器注册JCO服务监听需要调整 # server jco.server.connection_count=5 jco.server.gwh ...
- Spring之IOC原理及代码详解
一.什么是IOC 引用 Spring 官方原文:This chapter covers the Spring Framework implementation of the Inversion of ...
- List集合和Set集合互转
List集合转成Set集合(如果List集合的元素有重复,转成Set集合就会去掉重复的数据,每条数据只保留一条) //List转化成Set List<String> list = new ...
- Linux之redis的安装,主从配置
一,redis安装 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...
- Flutter——消息框(Fluttertoast)
引入依赖: dependencies: fluttertoast: ^3.1.3 import 'package:flutter/material.dart'; import 'package:flu ...
- TLS握手秘钥套件分析
1.为了弄清楚TLS1.3的内部结构,觉得有必要将TLS的整个结构从新整理一遍,方便后续在做握手协议的形式化分析的时候能够不遗漏每个加密和认证的的环节. TLS1.3不论文在协议内容上还是性能上都较之 ...
- roles
nginx 官方下载 [root@ansible roles]# tree yngx yngx ├── defaults ├── files │ ├── blog.conf │ ├── edu ...
- ngtos 天融信
NGFW系列产品基于天融信公司10年高品质安全产品开发经验结晶的NGTOS系统架构,采用了多项突破性技术.基于分层的设计思想,天融信公司通过长期的安全产品研发经验,分析多种安全硬件平台技术的差异,创造 ...