SPOJ - LCS2
后缀自动机板子题
https://vjudge.net/problem/28017/origin
找多串的最长公共子串
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod (1000000007)
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; char s[N];
int c[N],a[N];
struct SAM{
int last,cnt;
int ch[N<<][],fa[N<<],l[N<<];
int mx[N<<],tmp[N<<];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=;
else
{
int q=ch[p][c];
if(l[p]+==l[q])fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
}
void build(){
int len=strlen(s+);
last=cnt=;
for(int i=;i<=len;i++)ins(s[i]-'a');
topo();
for(int i=;i<=cnt;i++)mx[i]=l[i],tmp[i]=;
}
void topo(){
for(int i=;i<=cnt;i++)c[l[i]]++;
for(int i=;i<=cnt;i++)c[i]+=c[i-];
for(int i=;i<=cnt;i++)a[c[l[i]]--]=i;
}
void debug()
{
puts("");
for(int i=;i<=cnt;i++)
{
printf("%d ",i);
for(int j=;j<;j++)
printf("%d ",ch[i][j]);
printf("%d %d\n",fa[i],l[i]);
}
}
void faupdate()
{
for(int i=cnt;i;i--)
tmp[fa[a[i]]]=max(tmp[fa[a[i]]],tmp[a[i]]);
}
void maupdate()
{
for(int i=;i<=cnt;i++)
mx[i]=min(mx[i],tmp[i]),tmp[i]=;
}
void match()
{
int len=strlen(s+),val=,now=,res=;
for(int i=;i<=len;i++)
{
if(ch[now][s[i]-'a'])
{
now=ch[now][s[i]-'a'];
res++;
}
else
{
while(now&&!ch[now][s[i]-'a'])now=fa[now];
if(!now)now=,res=;
else
{
res=l[now]+;
now=ch[now][s[i]-'a'];
}
}
tmp[now]=max(tmp[now],res);
}
faupdate();
maupdate();
}
int maxlen()
{
int ans=;
for(int i=;i<=cnt;i++)ans=max(ans,mx[i]);
return ans;
}
}sam;
int main()
{
scanf("%s",s+);
sam.build();
while(~scanf("%s",s+))
sam.match();//,printf("%d\n",sam.maxlen());
printf("%d\n",sam.maxlen());
return ;
} /******************** ********************/
SPOJ - LCS2的更多相关文章
- 【spoj LCS2】 Longest Common Substring II
http://www.spoj.com/problems/LCS2/ (题目链接) 题意 求多个串的最长公共子串 Solution 对其中一个串构造后缀自动机,然后其它串在上面跑匹配.对于每个串都可以 ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- Virtual Judge SPOJ - LCS2 Longest Common Substring II
https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的. 注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理! ...
- 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 后缀自动机
多串的LCS,注意要利用拓扑序更新suf的len. 我用min,max,三目会超时,所以都改成了if,else #pragma warning(disable:4996) #include<cs ...
- 【SPOJ - LCS2】Longest Common Substring II【SAM】
题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...
- 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 ...
随机推荐
- IO流入门-第七章-BufferedReader
BufferedReader基本用法和方法示例 /* 字节 BufferedInputStream BufferedOutputStream 字符 BufferedReader:带有缓冲区的字符输入流 ...
- JavaScript四种数值取整方法
一.Math.trunc() 1.定义 Math.trunc()方法去除数字的小数部分,保留整数部分. 2.语法 Math.trunc(value) 3.示例 console.log(Math.tru ...
- 我的Android进阶之旅------>解决Android Studio编译后安装apk报错:The APK file does not exist on disk
1.错误描述 今天用Android Studio编译应用后安装APK的时候,报错了,错误如下所示: The APK file build\outputs\apk\OYP_2.3.4_I2Base_64 ...
- mysql 建立表之间关系 练习 2
创建数据库db6 create database db6 charset=utf8; user db6; # 创建班级表 mysql) not null unique); Query OK, rows ...
- 002-docker安装-mac上安装docker,17.06在CentOS7 64位机器上安装
一.mac上安装docker 1.下载 通过这个链接下载:https://download.docker.com/mac/stable/Docker.dmg 2.安装 将 Moby 的鲸鱼图标拖拽到 ...
- 通信—HTTP与HTTPS
HTTP是客户端浏览器或其他程序与Web服务器之间的应用层通信协议.在Internet上的Web服务器上存放的都是超文本信息,客户机需要通过HTTP协议传输所要访问的超文本信息. HTTPS(全称:H ...
- delphi webbrowser 获取iframe
procedure TForm1.Button4Click(Sender: TObject);var Index: Integer; Document: IHTMLDocument2; FrameId ...
- 转:[NHibernate]视图处理
转自:http://www.cnblogs.com/wolf-sun/p/4082899.html 目录 写在前面 文档与系列文章 视图 一个例子 总结 写在前面 前面的文章主要讲了对物理数据表的操作 ...
- Java基础教程:Lambda表达式
Java基础教程:Lambda表达式 本文部分内容引用自OneAPM:http://blog.oneapm.com/apm-tech/226.html 引入Lambda Java 是一流的面向对象语言 ...
- Keepalived 服务器状态监测
keepalived简介: keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器 ...