SPOJ 题目1811 LCS - Longest Common Substring(后缀自动机求最长公共子串)
来源:互联网 发布:最强nba球员数据 编辑:程序博客网 时间:2024/05/10 16:30
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:alsdfkjfjkdsalfdjskalajfkdslaOutput:3
sam确实快,sa超时了=_=,给a串建个sam,b串跑一遍就行了
ac代码
#include<stdio.h>#include<stdlib.h>#include<string.h>#define max(a,b) (a>b?a:b)#define N 510005 struct sam{sam *pre,*son[26];int len,g;}que[N],*root,*tail,*b[N];int tot;void add(int c,int l){sam *p=tail,*np=&que[tot++];np->len=l;tail=np;while(p&&p->son[c]==NULL){p->son[c]=np;p=p->pre;}if(p==NULL)np->pre=root;else{sam *q=p->son[c];if(p->len+1==q->len)np->pre=q;else{sam *nq=&que[tot++];*nq=*q;nq->len=p->len+1;np->pre=q->pre=nq;while(p&&p->son[c]==q){p->son[c]=nq;p=p->pre;} }}}char str1[N>>1],str2[N>>1];int dp[N>>1];int main(){while(scanf("%s%s",str1,str2)!=EOF){tot=0;root=tail=&que[tot++];int l=0;int len=strlen(str1),i;for(i=0;i<len;i++){add(str1[i]-'a',i+1);}len=strlen(str2);sam *p=root;int ans=0;for(i=0;i<len;i++){int now=str2[i]-'a';if(p->son[now]){p=p->son[now];l++;}else{while(p&&p->son[now]==NULL)p=p->pre;if(p==NULL){p=root;l=0;}else{l=p->len+1;p=p->son[now];}}if(l>ans)ans=l;}printf("%d\n",ans);}}
0 0
- SPOJ 题目1811 LCS - Longest Common Substring(后缀自动机求最长公共子串)
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
- SPOJ 1812 Longest Common Substring II 后缀自动机求多字符串最长公共子串
- SPOJ 题目1812 LCS2 - Longest Common Substring II(后缀自动机求多个串的最长公共子串)
- SPOJ LCS Longest Common Substring 后缀自动机
- [SPOJ LCS]Longest Common Substring && 后缀自动机
- 【后缀自动机】[SPOJ LCS]Longest Common Substring
- spoj 1811 LCS - Longest Common Substring (后缀自动机)
- 最长公共子串(Longest-Common-Substring,LCS)
- 最长公共子串(Longest-Common-Substring,LCS)
- 最长公共子串 Longest-Common-Substring(LCS)
- spoj1811 Longest Common Substring(LCS)最长公共子串
- hdu1403---Longest Common Substring(后缀数组求2个字符串的最长公共子串)
- 最长公共子串(Longest Common Substring, LCS)
- 【后缀自动机】SPOJ(LCS)[Longest Common Substring]题解
- spoj 1811 Longest Common Substring (后缀自动机)
- SPOJ-1811 Longest Common Substring(后缀自动机)
- SPOJ 1811 Longest Common Substring(后缀自动机)
- SQL语句功能介绍(1)
- 在Mac OS X系统下安装 Ruby运行环境详解
- 利用类反射模拟内省功能
- 理解angular中的module和injector,即依赖注入
- ctr c、ctrl d、ctrl z的区别
- SPOJ 题目1811 LCS - Longest Common Substring(后缀自动机求最长公共子串)
- 从Android源码分析View绘制
- JAVA问题之泛型数组
- 用css画的各种形状
- spring boot实战(第三篇)事件监听源码分析
- poj3661
- 【基于随机化的非确定性算法】模拟退火学习笔记
- Springmvc+ajax
- USACO 1.2 Dual Palindromes