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. Python的平凡之路(12)

    一.数据库介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数据存储在文件中,但 ...

  2. Java重载遇到泛型

    今天被问到一个有意思的问题,大家都知道重载的概念吧:一个类中定义同名的方法,参数表不同(参数类型,或者参数个数不通): 但是,如果是下面这个两个方法呢 public static int fn(Lis ...

  3. SP Flash Tool使用异常集锦

    1.The load scatter file is invalid无法载入scatter文件 (ubuntu下)我如果我们在使用MTK的Smart Phone Flash Tool过程中无法载入Sc ...

  4. 【转】Duff's Device

    在看strcpy.memcpy等的实现发现用了内存对齐,每一个word拷贝一次的办法大大提高了实现效率,参加该blog(http://totoxian.iteye.com/blog/1220273). ...

  5. BZOJ 4518 征途

    斜率优化.又是变量名打错看了老半天. 把方差式子展开一下就好了. #include<iostream> #include<cstdio> #include<cstring ...

  6. 【Python】str类方法说明

    #capitalize():字符串首字符大写 string = 'this is a string.'new_str = string.capitalize()print(new_str)#输出:Th ...

  7. 移动web中一些问题处理与事件说明

    1.1.1 所有盒子以边框开始计算 /*设置宽度以边框开始计算*/-webkit-box-sizing: border-box;box-sizing: border-box; 在移动端通常使用的是百分 ...

  8. 一键编译go文件命令.bat

    一键编译go文件命令.bat    , 请新建 一键编译go文件命令.bat    文件,放到你的xxx.go文件目录下 ( 欢迎加入go语言群: 218160862 , 群内有实践) 点击加入 @e ...

  9. 电脑重装BIOS设置中文翻译

  10. cookie httponly属性

    Marks the cookie as accessible only through the HTTP protocol. This means that the cookie won't be a ...