题目链接

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. 【颓废篇】人生苦短, 我用python(二)

    当时产生学习python的欲望便是在看dalao们写脚本的时候… 虽然dalao们好像用的是js来着.. 不过现在好像很多爬虫也可以用python写啊… 所以学python没什么不妥. 而且csdn整 ...

  2. JavaScript中获取HTML元素的方式

    JavaScript中获取HTML元素的方式 1.使用id方式获取元素,返回一个具体对象   document.getElementById(id名) 2.使用className方式获取元素,返回类数 ...

  3. leetcode-86-分割链表

    题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.va ...

  4. 容斥原理——状态压缩zoj3233 zoj2836升级版

    zoj2836就是裸的求lcm进行容斥,用dfs比较直观 zoj3233增加了一个集合b,lcm(b)的倍数是不符合条件的 那么在zoj2836的基础上,把lcm(x,lcm(b))造成的影响减去即可 ...

  5. LUOGU P2344 奶牛抗议 (树状数组优化dp)

    传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j- ...

  6. fiddler报错:creation of the root certificate was not successful 证书安装不成功

    fiddler提示:creation of the root certificate was not successful 证书安装不成功 首先 找到Tools——>Options 在弹出的菜单 ...

  7. js遍历获取表格内数据方法

    1.一般的表格结构如下 <table> <tr> <td>id</td> <td>name</td> </tr> & ...

  8. Maven的作用及简介

    Maven的作用及简介 一.maven作用 项目之间都是有依赖的,比如A项目依赖于B项目,B项目依赖与C.D项目,等等.这样的依赖链可能很长. 但是,没有一个项目的jar包我们都要导入进去,我们要做的 ...

  9. SPSS应用之非参数检验

    SPSS应用之非参数检验 统计学的假设检验可以分为参数检验和非参数检验,参数检验是根据一些假设条件推算而来,当这些假设条件无法满足的时候,参数检验的效能会大打折扣,甚至出现错误的结果,而非参数检验通常 ...

  10. 2019 西电ACM校赛网络赛 题解

    今年题目难度有较大提升,总体与往年类似,数学题居多.以下为我通过的部分题解. 赛题链接:http://acm.xidian.edu.cn/contest.php?cid=1053 A - 上帝视角 我 ...