地址: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. mysql远程连接授权

    Mysql:is not allowed to connect to this MySQL server 如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.1 ...

  2. 解析IE, FireFox, Opera 浏览器支持Alpha透明的方法

    先请看如下代码: filter:alpha(opacity=50);       /* IE */  -moz-opacity:0.5;              /* Moz + FF */  op ...

  3. ip地址查询系统和CMD查询的结果不一样

    由于cmd输入 ipconfig查看的IP是局域网内网IP,而用ip地址查看器查看是公网上网的ip地址.所以不一样. 查询内网ip: windows系统: 开始--运行--cmd,命令行输入: ipc ...

  4. Angular ContentChild

    contentchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-compone ...

  5. iOS文件路径相关的方法

    文件路径相关的方法在NSPathUtilities中,主要是操作路径 获得一个路径 NSString *documents = [NSSearchPathForDirectoriesInDomains ...

  6. 使用ProcDump工具抓取dump

    首先得到要抓取的进程号 cd %windir%\syswow64\inetsrvappcmd list wp得到pid之后, 在任务管理器里发现w3wp.exe的CPU总在49%-60%左右, 间歇性 ...

  7. linux命令之rpm

    1.查询一个包是否被安装的命令rpm -q  < rpm package name> 2.列出所有被安装的rpm package 命令rpm -qa

  8. Maven开发系统

    Maven的优点: 自动从互联网中获取jar包,并实现了一步构建. pom.xml的配置 依赖管理(导入对应的jar包) 通过坐标(定位到仓库中的包的位置,并将jar包导入到项目中,如果版本升级,只需 ...

  9. NIO概览

    NIO专题:http://developer.51cto.com/art/201112/307172.htm 一.新IO概述: 新IO和传统IO都是用于进行输入/输出,相比于传统IO面向流的处理方式, ...

  10. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...