地址: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. hdu 1520(简单树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:dp[u][0]表示不取u的最大价值,dp[u][1]表示取u的最大价值,于是有dp[u] ...

  2. 数据驱动ddt+excel数据读取

    我们可以将测试数据用excel存储,再用ddt去传入,不过我们需要安装对应的库,因为python是无法操作excel的 1.安装第三方库xlrd 2.创建一个excel表格,将需要测试的数据保存 3. ...

  3. Codeforces Round #296 (Div. 2) B. Error Correct System

    B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. 开发环境eclipse for Mac 下的常用快捷键汇总(基本参照Win系,将Ctrl换为Command)

    最近迁移开发环境到Mac下,在豆瓣看到一个常用快捷键,去掉废话直接上干货 Command + O:显示大纲 Command + 1:快速修复 Command + D:删除当前行 Command + O ...

  5. 【BZOJ4146】[AMPPZ2014]Divisors

    [BZOJ4146][AMPPZ2014]Divisors Description 给定一个序列a[1],a[2],...,a[n].求满足i!=j且a[i]|a[j]的二元组(i,j)的个数. In ...

  6. 用angular引入复杂的json文件2

    昨天我们也说了一下angular引入复杂json文件的方法,今天我们再来学习一种方法,而且更简单,更快捷. 首先我们引入一个angular插件,并且写上引入模块和控制台,在html中书写上模块名和控制 ...

  7. H5 localStorage入门

    定义 只读的 localStorage 允许你访问一个 Document 的远端(origin)对象 Storage:数据存储为跨浏览器会话.localStorage 类似于 sessionStora ...

  8. Spring Data @Query查询注解的使用(六)

    按照上一篇文章 我们知道  我们定义的方法  都要根据它的规范进行定义  不然就没法实用 这篇我们讲@Query 查询注解   我们就可以不需要遵循它的方法规则去编写  咱们讲@Query定义到方法上 ...

  9. c/c++分割字符串

    c++分割字符串: http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html c分割字符串: http://www.martinb ...

  10. CF#301 D:Bad Luck Island (概率dp)

    D:Bad Luck Island 一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j] ...