后缀自动机(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\)一样,就是 ...
随机推荐
- Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)
// VacationRequest.java /** * author : 冯孟活 ^_^ * dates : 2015年9月1日 下午10:32:58 * class : 演示简单的公司请假流程 ...
- Getting Started with Testing ——开始单元测试
Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as i ...
- MongoDB_1
突然想去看下MongoDB的东西,于是有了这篇文章.其实很早以前就看过一些关于NoSql的文章,还记得当时里面有介绍MongoDB的,多瞅了2眼,并且在Window下安装了MongoDB的驱动,小玩了 ...
- XCODE6 提交至 App Store
新到一个公司,以前的苹果开发人员离职,临时接手他的苹果代码,需要修改并上线到APP STORE. xcode6.0升级到最新的6.1后, 发现各种坑 1. 路径配置不对, 这个是个人习惯问题,之前的 ...
- J2se中的声音---AudioPlayer
1 package cn.gp.tools; import java.io.FileInputStream; import java.io.FileNotFoundException; import ...
- 通过移位与或非运算获取整形最大值,最小值,以及获取输入的int类型整数的二进制表示
以上是最终效果 实现类: package com.corejava.chap02; public class IntBin { private int value; public IntBin(int ...
- c语言字符数组和指针的经典用法
1.字符数组 许多情况下,对字符串的处理使用字符数组会更加方便,比如: 我觉得不改变字符串的原有顺序,对字符串进行删除等操作时,使用字符数组效果会更好. eg:给定字符串(ASCII码0-255)数组 ...
- 使用Eclipse搭建C/C++开发环境(转)
使用Eclipse搭建C/C++开发环境 文章出自:http://www.cnblogs.com/liuxianan/archive/2013/01/15/2861196.html 说明:网上有很多 ...
- 【BZOJ2809】【splay启发式合并】dispatching
Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...
- 【POJ3580】【splay版】SuperMemo
Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...