后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II
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 这题是找一些字符串的最长公共子串。
这里对其中一个建SAM,然后跑其他字符串,在每一个字符串中,记录SAM节点中每个节点对应的最长长度,最后在所有字符串中SAM的最长长度取MIN,答案最后再在对每个位置的最小值取MAX。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; struct SAM{
int ch[][],len[],fa[],last,cnt;
void Init()
{
memset(fa,,sizeof(fa));
last=cnt=;
}
void Insert(int c)
{
int p=last,np=last=++cnt;
len[np]=len[p]+;
while(p&&!ch[p][c])
ch[p][c]=np,p=fa[p];
if(!p)
fa[np]=;
else{
int q=ch[p][c];
if(len[p]+==len[q])
fa[np]=q;
else{
int nq=++cnt;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
len[nq]=len[p]+;
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
while(p&&ch[p][c]==q)
ch[p][c]=nq,p=fa[p];
}
}
}
}sam;
char s[];
int ans[],f[];
int main()
{
memset(ans,,sizeof(ans));
sam.Init();
scanf("%s",&s);
for(int i=;s[i];i++)
sam.Insert(s[i]-'a');
while(~scanf("%s",s))
{
memset(f,,sizeof(f));
int node=,len=;
for(int i=;s[i];i++)
{
int c=s[i]-'a';
while(node&&!sam.ch[node][c])
node=sam.fa[node];
if(!node)
node=,len=;
else
len=sam.len[node]+,node=sam.ch[node][c];
if(len>f[node])f[node]=len;
}
for(int i=;i<=sam.cnt;i++)
ans[i]=min(ans[i],f[i]);
}
int ret=;
for(int i=;i<=sam.cnt;i++)
ret=max(ret,ans[i]);
printf("%d\n",ret);
return ;
}
后缀自动机(SAM):SPOJ Longest Common Substring II的更多相关文章
- 2018.12.15 spoj Longest Common Substring II(后缀自动机)
传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...
- SPOJ Longest Common Substring II
题目连接:戳我 题目大意:求n个字符串的最长公共子串. 它的简化版--这里 当然我们可以用SA写qwq,也可以用广义SAM写qwq 这里介绍纯SAM的写法...就是对其中一个建立后缀自动机,然后剩下的 ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- Longest Common Substring II SPOJ - LCS2 (后缀自动机)
Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...
- 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 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
随机推荐
- c#参数传递使用中的一个坑,值传递与引用传递
c#参数传递使用中发现的一个问题 写了3个重载方法,把 对象.int .(int直接封入object) 传入SWAP方法进行数据操作结果对象内的数据发生了改变,其他2个没有:
- sqlite使用blob类型存储/访问 结构体
/* open fire host and slora report data database */ int open_report_db(void) { ; char sql[SQL_COMMAN ...
- iptables学习笔记
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3812510.html ...
- 23、Javascript DOM
DOM Document Object Model(文档对象模型)定义了html和xml的文档标准. DOM 节点树 <html> <head> <title>DO ...
- 乐视手机查看运行内存方法、EUI(Eco User Interface)乐视系统查看手机运行内存方法
点击按钮,左下角,方格, 显示如下:
- LENGTH和LENGTHB函数,substrb截取也是同一个道理。
oracle 利用 LENGTH和LENGTHB函数区分中英文(2009-02-07 10:49:29) 转载▼ 标签: it 分类: oracle 前一段时间,我一朋友问我怎么得出这个字符串是中文还 ...
- XFire中Services.xml 配置的一些细节
昨天第一次调XFire引擎,放在Tomcat里之后老是报错,检查了很久没有发现什么问题,一直很费解. 后来在网上看到一篇文章<XFire services.xml 配置文件补充说明>,根绝 ...
- const详解
详解C++中的const关键字
- apple iphone 3gs 有锁机 刷机 越狱 解锁 全教程(报错3194,3014,1600,短信发不出去等问题可参考)
以自身经历列步骤如下:(基本思路就是刷6.1.6,越狱,降级基带,解锁) 一.准备工作 1.下载3gs 6.1.6官方固件.地址:http://act.feng.com/wetools/index.p ...
- 用Raphael在网页中画圆环进度条
原文 :http://boytnt.blog.51cto.com/966121/1074215 条状的进度条我们见得太多了,实现起来比较简单,它总是长方形的,在方形的区域里摆 放就不太好看了.随着cs ...