Description

Given two strings, you have to tell the length of the Longest Common Substring of them.

For example: 
str1 = banana 
str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.

Input

The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.

Output

For each test case, you have to tell the length of the Longest Common Substring of them. 

Sample Input

banana
cianaic

Sample Output

3

题目大意:给两个字符串,求最长的公共子串的长度。
题目分析:扫一遍height数组即可。 代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<cmath>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std; const int N=100000; int SA[N*2+5];
int cnt[N*2+5];
int rk[N*2+5];
int tSA[N*2+5];
int height[N*2+5];
int n;
string str; bool same(int i,int j,int k)
{
if(tSA[i]!=tSA[j]) return false;
if(i+k>=n&&j+k<n) return false;
if(i+k<n&&j+k>=n) return false;
return tSA[i+k]==tSA[j+k];
} void buildSA(string s)
{
int m=27;
n=s.size();
for(int i=0;i<m;++i) cnt[i]=0;
for(int i=0;i<n;++i) ++cnt[rk[i]=s[i]-'a'];
for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;--i) SA[--cnt[rk[i]]]=i;
for(int k=1;k<=n;k<<=1){
int p=0;
for(int i=n-k;i<n;++i) tSA[p++]=i;
for(int i=0;i<n;++i) if(SA[i]>=k) tSA[p++]=SA[i]-k; for(int i=0;i<m;++i) cnt[i]=0;
for(int i=0;i<n;++i) ++cnt[rk[tSA[i]]];
for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;--i) SA[--cnt[rk[tSA[i]]]]=tSA[i]; swap(rk,tSA);
p=1;
rk[SA[0]]=0;
for(int i=1;i<n;++i)
rk[SA[i]]=same(SA[i],SA[i-1],k)?p-1:p++;
if(p>=n) break;
m=p;
}
} void getHeight()
{
for(int i=0;i<n;++i) rk[SA[i]]=i;
int k=0;
for(int i=0;i<n;++i){
if(rk[i]==0){
height[rk[i]]=k=0;
}else{
if(k) --k;
int j=SA[rk[i]-1];
while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) ++k;
height[rk[i]]=k;
}
}
} string str1,str2; bool diff(int i,int j,int m)
{
if(SA[i]==m||SA[j]==m) return false;
return (SA[i]-m)/abs(SA[i]-m)*(SA[j]-m)/abs(SA[j]-m)<0;
} int f(int m)
{
int ans=0;
for(int i=1;i<n;++i){
if(diff(i,i-1,m)&&height[i]>ans)
ans=height[i];
}
return ans;
} int main()
{
while(cin>>str1>>str2)
{
str=str1+(char)('z'+1)+str2;
buildSA(str);
getHeight();
//cout<<"here is good"<<endl;
printf("%d\n",f(str1.size()));
}
return 0;
}

  


HDU 1403-Longest Common Substring (后缀数组)的更多相关文章

  1. hdu 1403 Longest Common Substring 后缀数组 模板题

    题目链接 题意 问两个字符串的最长公共子串. 思路 加一个特殊字符然后拼接起来,求得后缀数组与\(height\)数组.扫描一遍即得答案,注意判断起始点是否分别在两个串内. Code #include ...

  2. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...

  3. HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)

    Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...

  4. HDU - 1403 - Longest Common Substring

    先上题目: Longest Common Substring Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. HDU 1403 Longest Common Substring(后缀数组,最长公共子串)

    hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...

  6. HDU 1403 Longest Common Substring(最长公共子串)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所 ...

  7. POJ 2774 Long Long Message&&HDU 1403 Longest Common Substring&&COJ 1203

    后缀数组的买1送2题... HDU的那题数据实在是太水了,后来才发现在COJ和POJ上都是WA..原因在一点:在建立sa数组的时候里面的n应该是字符串长度+1....不懂可以去看罗大神的论文... 就 ...

  8. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  9. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  10. hdu_1403_Longest Common Substring(后缀数组的应用)

    题目链接:hdu_1403_Longest Common Substring 题意: 给你两个字符串,然你找最长的公共子串 题解: 后缀数组的经典应用,要找两个字符串的公共子串,那么就相当于找两个串的 ...

随机推荐

  1. unity代码加密for Android,mono编译

    uinty3d加密推荐几篇比较好的博客链接: http://www.cppcourse.com/u3d-encryption.html http://www.xuanyusong.com/archiv ...

  2. C# WebBrowser控件使用教程与技巧收集

    常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(strin ...

  3. asp.net生成随机密码

    public string RandCode(int n) { char[] arrChar = new char[]{ 'a','b','d','c','e','f','g','h','i','j' ...

  4. [DNS-BIND]网络初始化

    1.创建ns_g_socketmgr: 首先,套接字管理器是全局唯一的,与有多少个网络接口无关,全局变量定义在/bin/named/include/named/globals.h: EXTERN is ...

  5. JSHelper时间格式化

    Helper.prototype.FormatDate = function (format) { var _now = new Date(); var o = { "M+": _ ...

  6. 摆花(2012Noip普及组第3题)

    摆花 (flower.cpp/c/pas) [问题描述] 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆.通过调查顾客的喜好,小明列出了顾客最喜欢的 n 种花,从 1 到 n ...

  7. Java并发编程学习笔记(三)——对象的组合

    重要概念: 1.在设计线程安全类的过程中,需要包含以下三个基本要素: (1)找出构成对象状态的所有变量. (2)找出约束状态变量的不变性条件. (3)建立对象状态的并发访问管理策略. 2.

  8. HTML标签整理

    第一次接触动态网站的相关代码,对程序里HTML的标签不理解.在这里会把碰到的HTML标签整理出来,持续更新. 1.<form></form>:用于声明表单,定义采集数据的范围, ...

  9. Protobuf C#教程 ThriftC#教程大合辑

    android与PC,C#与Java 利用protobuf 进行无障碍通讯[Socket] http://www.cnblogs.com/TerryBlog/archive/2011/04/23/20 ...

  10. 开源框架中常用的php函数

    类的自动加载后直接实例化 //自动加载类 function my_autoloader($class) { include $class . 'Class.php'; } spl_autoload_r ...