spoj1812-Longest Common Substring II(后缀自动机)
Description
A string is finite sequence of characters over a non-empty finite set Σ.
In this problem, Σ is the set of lowercase letters.
Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task is a bit harder, for some given strings, find the length of the longest common substring of them.
Here common substring means a substring of two or more strings.
Input
The input contains at most 10 lines, each line consists of no more than 100000 lowercase letters, representing a string.
Output
The length of the longest common substring. If such string doesn't exist, print "0" instead.
Example
Input:
alsdfkjfjkdsal
fdjskalajfkdsla
aaaajfaaaa Output:
2
题意:给若干个字符串,长度不超过100000,求最长公共连续字串
解析:后缀自动机,对输入的第一个字符串建一个后缀自动机,在
sam中添加两个量Max[i](查询时以第i个节点为最后一个字符的后缀的最大长度)
,Min[i](所有查询Max[i]中的最小值)。
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=;
struct SAM
{
int ch[maxn][];
int pre[maxn],step[maxn];
int last,id;
int Min[maxn],Max[maxn];
void init() //初始化
{
last=id=;
memset(ch[],-,sizeof(ch[]));
pre[]=-; step[]=;
Min[]=Max[]=;
}
void Insert(int c) //模板,自己百度
{
int p=last,np=++id;
step[np]=step[p]+;
memset(ch[np],-,sizeof(ch[np]));
Min[np]=Max[np]=step[np];
while(p!=-&&ch[p][c]==-) ch[p][c]=np,p=pre[p];
if(p==-) pre[np]=;
else
{
int q=ch[p][c];
if(step[q]!=step[p]+)
{
int nq=++id;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
step[nq]=step[p]+;
Min[nq]=Max[nq]=step[nq];
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
while(p!=-&&ch[p][c]==q) ch[p][c]=nq,p=pre[p];
}
else pre[np]=q;
}
last=np;
}
void Find(char *S)
{
int len=strlen(S);
int u=,d=;
for(int i=;i<=id;i++) Max[i]=;
for(int i=;i<len;i++)
{
int c=S[i]-'a';
if(ch[u][c]!=-) d++,u=ch[u][c];
else
{
while(u!=-&&ch[u][c]==-) u=pre[u];
if(u!=-) d=step[u]+,u=ch[u][c];
else u=,d=;
}
Max[u]=max(Max[u],d);//更新
}
for(int i=id;i>=;i--) Max[pre[i]]=max(Max[pre[i]],Max[i]);
for(int i=;i<=id;i++) Min[i]=min(Min[i],Max[i]);
}
int GetAns()
{
int ret=;
for(int i=;i<=id;i++) ret=max(ret,Min[i]);
return ret;
}
}sam;
char S[maxn];
int main()
{
scanf("%s",S);
int len=strlen(S);
sam.init();
for(int i=;i<len;i++) sam.Insert(S[i]-'a');
while(scanf("%s",S)!=EOF) sam.Find(S);
printf("%d\n",sam.GetAns());
return ;
}
spoj1812-Longest Common Substring II(后缀自动机)的更多相关文章
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- 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 ...
- 2018.12.15 spoj1812 Longest Common Substring(后缀自动机)
传送门 后缀自动机模板题. 题意简述:求两个字串的最长公共子串长度. 对其中一个构建后缀自动机,用另外一个在上面跑即可. 代码: #include<bits/stdc++.h> #defi ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- SPOJ1812 Longest Common Substring II
题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
随机推荐
- JFreeChart学习
一.步骤: 创建数据集(准备数据) 根据数据集生成JFreeChart对象,并对其做相应的设置(标题,图例,x轴,Y轴,对象渲染等) 将JFreeChart对象输出到文件或者Servlet输出流等 二 ...
- Truncate Table 用法
TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作. 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 注释 TRUNC ...
- 简易封装一个带有占位文字的TextView
在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:
- css样式:列表
css code: /*系统自带的*/ ul li{ list-style-style: disc; } /*自定义图标*/ ul li{ list-style-image: url("im ...
- localStrorage、 sessionStorage 、cookie
HTML5中增加了两种全新数据存储方式:Web Storage和Web SQL Database. 前者可用于临时或永久保存客户端的少量数据:后者是客户端本地化的一套数据库系统,可将大量数 ...
- centos虚拟机NAT静态IP设置
宿主机为Centos6.3 64位,三台虚拟机为为Centos6.3 64位.虚拟机的网络连接方式为默认的NAT方式.虚拟机默认为DHCP方式动态获取IP.为了在三台虚拟机上搭建hadoop,需要将这 ...
- Ubuntu 搭建NDK环境
一. NDK下载地址 https://developer.android.com/tools/sdk/ndk/index.html 二. NDK环境两种方式 NDK下载后,解压缩后放置于目录/home ...
- Linux下rar unrar的安装
Linux下rar unrar的安装: 以3.8.0版本为例,如果是64位平台,执行以下命令,也可以去官方网站:)下载最新版: wget http://www.rarlab.com/rar/rarli ...
- rpm软件包类型
rpm软件包 在linux世界里有两种流行的包管理方式,分别是redhat系的rpm和debian系的deb.其中rpm是RedHat Package Manager(RedHat软件包管理工具)的简 ...
- MVC 5 下,应用log4net收集异常信息
1.安装log4net. 首先在VS中通过nuget安装logenet,我用的是VS2013,截屏如下: