地址: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. 【python】NLTK好文

    From:http://m.blog.csdn.net/blog/huyoo/12188573 nltk是一个python工具包, 用来处理和自然语言处理相关的东西. 包括分词(tokenize), ...

  2. 使用spring + ActiveMQ 总结

    使用spring + ActiveMQ 总结   摘要 Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 目录[-] Spring 整合JMS 基于ActiveMQ 实现消息的发送接 ...

  3. 目标检测YOLOv1-v3——学习笔记

    Fast RCNN更准一些.其损失函数比YOLO简单. YOLO更快 YOLO(You Only Look Once) 简介: 测试过程: 训练过程: 坐标.含有.不含.类别预测 目标检测的效果准确率 ...

  4. PHP的只是结构图

  5. EF简单查询

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. 电力项目七--js控制文字内容过长的显示和文本字数的显示

    当文本框中文字内容过长时,需要调整显示的样式 如上图所示的样式 对应的代码为: <div id="showInfomation" style="visibility ...

  7. 第十一课——codis-server的高可用,对比codis和redis cluster的优缺点

    [作业描述] 1.配置codis-ha 2.总结对比codis的集群方式和redis的cluster集群的优缺点 =========================================== ...

  8. hdu4686 简单的矩阵快速幂求前n项和

    HDU4686 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意:题目说的很清楚了,英语不好的猜也该猜懂了,就是求一个表达式的前n项和,矩阵 ...

  9. 污染Bootstrap modal 通过 css选择器 避免

    w 对框架的掌握.改进. 0-存在重复代码,需要改正,js timepicker框架传入类名: 1-大量的点击块,怎样避免对每个块重复写modal? <style> .w > td ...

  10. Python3: Command not found(Mac OS)

    1. 第一步:查看以下路径是否安装有 Python 3.x # 打开以下目录, 版本号有可能不同 cd /usr/local/Cellar/python/3.5.2_3/bin # 查看当前目录的内容 ...