题目链接

Longest Common Substring

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

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
题意:求两个字符串的最长公共字串。
思路:如果说要求一个字符串中至少出现两次的长度最大的字串,那么结果就是后缀数组中相邻的两个后缀的高度
的最大值。也就是max(lcp[i], lcp[i+1]). 因此可以将两个字符串用一个不会出现的字符连接起来(如$)
Accepted Code:

 /*************************************************************************
> File Name: 1403.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月22日 星期五 09时00分05秒
> Propose: sa + lcp
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int MAX_N = << ;
int n, k;
int sa[MAX_N], lcp[MAX_N], rank[MAX_N], tmp[MAX_N]; bool compare_sa(int i, int j) {
if (rank[i] != rank[j]) return rank[i] < rank[j];
  else {
int ri = i + k <= n ? rank[i + k] : -;
int rj = j + k <= n ? rank[j + k] : -;
return ri < rj;
}
} void construct_sa(const string &S, int *sa) {
n = S.length(); for (int i = ; i <= n; i++) {
sa[i] = i;
rank[i] = i < n ? S[i] : -;
  }  for (k = ; k <= n; k *= ) {
sort(sa, sa + n + , compare_sa); tmp[sa[]] = ;
for (int i = ; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - ]] + (compare_sa(sa[i - ], sa[i]) ? : );
}
for (int i = ; i <= n; i++) rank[i] = tmp[i];
}
} void construct_lcp(const string &S, int *sa, int *lcp) {
int n = S.length();
for (int i = ; i <= n; i++) rank[sa[i]] = i; int h = ;
lcp[] = ;
for (int i = ; i < n; i++) {
int j = sa[rank[i] - ]; if (h > ) h--;
for (; j + h < n && i + h < n; h++) if (S[i + h] != S[j + h]) break; lcp[rank[i] - ] = h;
}
} string S, T; void solve() {
int len1 = S.length();
S = S + '$' + T; construct_sa(S, sa);
construct_lcp(S, sa, lcp); int ans = ;
for (unsigned i = ; i < S.length(); i++) {
if ((sa[i] < len1) != (sa[i + ] < len1))
ans = max(ans, lcp[i]);
}
cout << ans << endl;
} int main(void) {
ios::sync_with_stdio(false);
while (cin >> S >> T) {
solve();
}
return ;
}

Hdu 1403(后缀数组)的更多相关文章

  1. HDU - 1403 后缀数组初步

    题意:求两个串的最长公共子串 两个串连接起来然后求高度数组 注意两个sa值必须分别在不同一侧 本题是用来测试模板的,回想起青岛那次翻车感觉很糟糕 #include<iostream> #i ...

  2. hdu 3948 后缀数组

    The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  3. hihoCoder 1403 后缀数组一·重复旋律(后缀数组+单调队列)

    #1403 : 后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成 ...

  4. HDU - 3948 后缀数组+Manacher

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...

  5. HDU 5769 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 [2016多校contest-4] 题意:给定一个字符,还有一个字符串,问这个字符串存在多少个不 ...

  6. hdu 2459 (后缀数组+RMQ)

    题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...

  7. hdu 3518(后缀数组)

    题意:容易理解... 分析:这是我做的后缀数组第一题,做这个题只需要知道后缀数组中height数组代表的是什么就差不多会做了,height[i]表示排名第i的后缀与排名第i-1的后缀的最长公共前缀,然 ...

  8. hdu 5442 (后缀数组)

    稍微学习了下第一次用后缀数组- - , 强行凑出答案 , 感觉现在最大的问题是很多算法都不知道 ,导致有的题一点头绪都没有(就像本题).  /*推荐 <后缀数组——处理字符串的有力工具>— ...

  9. HIHOcoder 1403 后缀数组一·重复旋律

    思路 后缀数组的板子题,注意后缀数组的rank[]数组是通过位置找到对应排名的,sa[]是通过排名找到位置的,height[i]记录的是sa[i]和sa[i+1]之间的lcp 不要写错了就行了 代码 ...

随机推荐

  1. jmeter遇到的问题之Windows读取jtl文件出错

    问题描述 ① 使用linux运行jmeter.jmx文件后生成result.jtl文件 jmeter -n -t /tmp/jmeter.jmx -l /tmp/testresult/result.j ...

  2. Python3基础笔记_元组

    # Python3 元组 ''' Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运 ...

  3. Error configuring application listener of class [org.springframework.web.util.Log4jConfigListener]

    1.启动项目发现如下错误: 严重: Error configuring application listener of class [org.springframework.web.util.Log4 ...

  4. js实现获取两个日期之间所有日期的方法

    function getDate(datestr){ var temp = datestr.split("-"); var date = new Date(temp[0],temp ...

  5. 如何用maven读取本地架包

    一.这篇微博是建立在已经把架包复制过来的情况下的.其余情况另外谈. 二.本篇文章用的是cmd控制的 maven的安装请看以下链接:https://jingyan.baidu.com/article/3 ...

  6. xml中的<if>和截取字符串

    <#if (envPollute=='1')>√</#if><#if (envPollute=='0')>√</#if>${as_title?subst ...

  7. 【LGP4389】付公主的背包

    题目 退役前抄一道生成函数快乐一下 就是让我们做一个完全背包,但是朴素的做法显然是\(O(nm)\)的 把每一个物品搞成一个多项式,显然这个多项式所有\(v_i\)的倍数箱为\(1\),剩下的为\(0 ...

  8. C#绘制渐变背景

    //绘制渐变色背景 Graphics g = e.Graphics; LinearGradientBrush linearGradientBrush = new LinearGradientBrush ...

  9. C开发系列-指针

    指针 通过一段简单的程序,引入指针的概念 #include <iostream> using namespace std; // changeValue函数的定义 void changeV ...

  10. Spring Cloud各组件

    讲的不错:http://www.ityouknow.com/springcloud/2017/05/16/springcloud-hystrix.html Spring Cloud技术应用从场景上可以 ...