【刷题】SPOJ 1811 LCS - Longest Common Substring
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 simple, for two 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 exactly two lines, each line consists of no more than 250000 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
Output:
3
Solution
做字符串题根SPOJ打交道很多啊
SAM模板,并get新技能,一个串的SAM与另一个串的匹配
将一个串的SAM建好之后,枚举另一个串的字符,如果可以直接匹配就直接匹配,如果直接匹配不了,那么将SAM的指针不停地往上跳,使得代表字符串的长度越来越小,以便能够匹配
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=250000+10;
int n1,n2,tot=1,las=1,ch[MAXN<<1][30],len[MAXN<<1],fa[MAXN<<1],size[MAXN<<1],ans;
char s1[MAXN],s2[MAXN];
template<typename T> inline void read(T &x)
{
	T data=0,w=1;
	char ch=0;
	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
	if(ch=='-')w=-1,ch=getchar();
	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
	x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
	if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void extend(int c)
{
	int p=las,np=++tot;
	las=np;
	len[np]=len[p]+1;
	while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
	if(!p)fa[np]=1;
	else
	{
		int q=ch[p][c];
		if(len[q]==len[p]+1)fa[np]=q;
		else
		{
			int nq=++tot;
			fa[nq]=fa[q];
			memcpy(ch[nq],ch[q],sizeof(ch[nq]));
			len[nq]=len[p]+1;
			fa[np]=fa[q]=nq;
			while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];
		}
	}
	size[np]=1;
}
int main()
{
	scanf("%s%s",s1+1,s2+1);
	n1=strlen(s1+1),n2=strlen(s2+1);
	for(register int i=1;i<=n1;++i)extend(s1[i]-'a'+1);
	for(register int i=1,j=1,res=0,c;i<=n2;++i)
	{
		c=s2[i]-'a'+1;
		if(ch[j][c])res++,j=ch[j][c];
		else
		{
			while(j&&!ch[j][c])j=fa[j];
			if(!j)res=0,j=1;
			else res=len[j]+1,j=ch[j][c];
		}
		chkmax(ans,res);
	}
	write(ans,'\n');
	return 0;
}
【刷题】SPOJ 1811 LCS - Longest Common Substring的更多相关文章
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
		spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ... 
- SPOJ 1811 LCS - Longest Common Substring
		思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ... 
- spoj1811 LCS - Longest Common Substring
		地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ... 
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
		LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ... 
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
		spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ... 
- 【SP1811】LCS - Longest Common Substring
		[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ... 
- SPOJ 10570 LONGCS - Longest Common Substring
		思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成多组数据就有三倍经验了 代码 #include <cstdio> #inclu ... 
- 【刷题】SPOJ 1812 LCS2 - Longest Common Substring II
		A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ... 
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
		Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ... 
随机推荐
- cakephp2.x 多个应用程序公用一个核心类库
			环境Windows,apache,cake版本2.3.3 Cake项目路径 D:\wamp\www\Mycakephp 浏览器打开 http://localhost/Mycakephp 能够正常访问到 ... 
- 问题集 - console.log在IE下不可用
			js中添加如下一段代码即可. if(!window.console){ window.console = {}; } if(!window.console.log){ window.console.l ... 
- 「日常训练」Maximum Multiple(HDU-6298)
			题意与分析 一开始以为是一条高深的数学题,跳过去了,后来查其他题目的代码的时候无意看到,一看emmmmmm 稍微思考一下就有了.\(1=\frac{1}{3}+\frac{1}{3}+\frac{1} ... 
- [JSON].valueOf( keyPath )
			语法:[JSON].valueOf( keyPath ) 返回:[任意类型 | null] 说明:获取键名路径原值,它保留原始值的类型 示例: b = sysFile.binary("tes ... 
- JS原型链与继承别再被问倒了
			原文:详解JS原型链与继承 摘自JavaScript高级程序设计: 继承是OO语言中的一个最为人津津乐道的概念.许多OO语言都支持两种继承方式: 接口继承 和 实现继承 .接口继承只继承方法签名,而实 ... 
- [Clr via C#读书笔记]Cp5基元类型引用类型值类型
			Cp5基元类型引用类型值类型 基元类型 编译器直接支持的类型,基元类型直接映射到FCL中存在的类型. 作者希望使用FCL类型名称而避免使用关键字.他的理由是为了更加的清晰的知道自己写的类型是哪种.但是 ... 
- Python3 Tkinter-Radionbutton
			1.创建单选按钮 from tkinter import * root=Tk() Radiobutton(root,text='b1').pack() Radiobutton(root,text='b ... 
- Python中除法:/和//
			在Python中,除法有两种:/和//. X / Y 对于Python2.X来说,如果两个操作数都是整数,那么结果将向下取整(这个和C里面的不同,C里面是向0取整),也就是说,如果结果本来是-2.5, ... 
- NYOJ  35 表达式求值(逆波兰式求值)
			http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ... 
- Thunder团队第一周 - Scrum会议4
			Scrum会议4 小组名称:Thunder 项目名称:爱阅app Scrum Master:代秋彤 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ... 
