地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1403

题目:

Longest Common Substring

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6296    Accepted Submission(s): 2249

Problem 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
 
Author
Ignatius.L
 

思路:把两个字符串连接起来,中间用一个没出现过的字符隔开。

  然后二分答案,二分check时对height进行分组,判断height值全大于x的组内 是否同时包含两个字符串的子串

  

 #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm> const int N = ;
int sa[N],s[N],wa[N], wb[N], ws[N], wv[N];
int rank[N], height[N]; bool cmp(int r[], int a, int b, int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
} void da(int r[], int sa[], int n, int m)
{
int i, j, p, *x = wa, *y = wb;
for (i = ; i < m; ++i) ws[i] = ;
for (i = ; i < n; ++i) ws[x[i]=r[i]]++;
for (i = ; i < m; ++i) ws[i] += ws[i-];
for (i = n-; i >= ; --i) sa[--ws[x[i]]] = i;
for (j = , p = ; p < n; j *= , m = p)
{
for (p = , i = n - j; i < n; ++i) y[p++] = i;
for (i = ; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = ; i < n; ++i) wv[i] = x[y[i]];
for (i = ; i < m; ++i) ws[i] = ;
for (i = ; i < n; ++i) ws[wv[i]]++;
for (i = ; i < m; ++i) ws[i] += ws[i-];
for (i = n-; i >= ; --i) sa[--ws[wv[i]]] = y[i];
for (std::swap(x, y), p = , x[sa[]] = , i = ; i < n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p- : p++;
}
} void calheight(int r[], int sa[], int n)
{
int i, j, k = ;
for (i = ; i <= n; ++i) rank[sa[i]] = i;
for (i = ; i < n; height[rank[i++]] = k)
for (k?k--:, j = sa[rank[i]-]; r[i+k] == r[j+k]; k++);
}
bool check(int la,int lb,int lc,int x)
{
int m1=,m2=;
if(sa[]<la)m1=;
if(sa[]>la)m2=;
for(int i=;i<=lc;i++)
{
if(height[i]<x)
{
if(m1&&m2)
return ;
m1=m2=;
}
if(sa[i]<la)m1=;
if(sa[i]>la)m2=;
}
return m1&&m2;
}
char ss[N];
int main()
{
while(scanf("%s",ss)==)
{
int la=strlen(ss),lb,n=;
for(int i=;i<la;i++)
s[n++]=ss[i]-'a'+;
s[n++]=;
scanf("%s",ss);
lb=strlen(ss);
for(int i=;i<lb;i++)
s[n++]=ss[i]-'a'+;
s[n]=;
da(s,sa,n+,);
calheight(s,sa,n);
int l=,r=la,ans=;
while(l<=r)
{
int mid=l+r>>;
if(check(la,lb,n,mid))
ans=mid,l=mid+;
else
r=mid-;
}
printf("%d\n",ans);
}
return ;
}

hdu1403 Longest Common Substring的更多相关文章

  1. [HDU1403]Longest Common Substring(后缀数组)

    传送门 求两个串的公共子串(注意,这个公共子串是连续的一段) 把两个串连在一起,中间再加上一个原字符串中不存在的字符,避免过度匹配. 求一遍height,再从height中找满足条件的最大值即可. 为 ...

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

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

  3. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  4. LintCode Longest Common Substring

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-substring/# 题目: Given two strings, find th ...

  5. Longest Common Substring

    Given two strings, find the longest common substring. Return the length of it. Example Given A = &qu ...

  6. 【SPOJ】1812. Longest Common Substring II(后缀自动机)

    http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要 ...

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

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

  8. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  9. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

随机推荐

  1. [转]Linux动态库的种种要点

    linux下使用动态库,基本用起来还是很容易.但如果我们的程序中大量使用动态库来实现各种框架/插件,那么就会遇到一些坑,掌握这些坑才有利于程序更稳健地运行. 本篇先谈谈动态库符号方面的问题. 测试代码 ...

  2. MFC存储图片到SQL Server数据库

    第一步:建立数据库表,比如:id char,pic image. 第二步:建立MFC单文档应用程序,再添加类CMyRecordset,基类选择CRecordset,导入数据库的刚建立的表. 第三步:在 ...

  3. elasticsearch性能因素总结

    一:硬件方面   在预算充足的情况下.特别是一些高并发业务的搜索.硬件层面占用整个elasticsearch性能空间很大比例. 1)内存: 单实例的情况下,尽量分配32G,排序和统计都是以及内存计算的 ...

  4. php求数学对数

    php的对数函数并不是很强大 有自然对数 有10的对数的函数,不过没有自定义底的对数函数,所以自己写了一个 <?php function xsqrt($x, $value) { $count = ...

  5. measure layout onMeasure() onLayout()

    1.onMeasure() 在这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的 measure(newWidthMeasureSpec, heightM ...

  6. LA5059 Playing With Stones

    题意:nim游戏.加上限制每次不得取走超过当前堆一半的石子 1 ≤ N ≤ 100,1 ≤ ai ≤ 2 ∗ 1018 分析:由于ai过大.所以我们采用SG函数递推找规律. (详见代码) #inclu ...

  7. 如何让listview滚动到底部

    方法一: // msgListView是ListView控件 // adapter是ListView绑定的Adapter,如果不方便直接使用,也可以通过ListView的getAdapter()方法获 ...

  8. 设置 SSH Key 登录服务器和 Git 服务器

    设置 SSH Key 登录服务器 通过 ssh 登录服务器,一直都是用的账号和密码,今天看到一篇文章说这样不安全,使用 ssh key 的方式登录则是更好的选择,因此,研究实践了一下,并记录在这里. ...

  9. 桌面不显示IE图标解决方法

    如果你的是WIN7,恰好你的桌面上IE图标不知道怎么被搞咩了. 向这里看过来,这里有一个文件你正好能用上. 使用方法很简单: 猛戳下载,猛戳两下安装 下载解决

  10. 配置Hibernate的流程

    配置项目的前提下你应该配置好你的开发环境 1新建hibernate.cfg.xml文件,放在src目录里 <?xml version="1.0" encoding=" ...