题目链接

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. Caffe系列2——Windows10制作LMDB数据详细过程(手把手教你制作LMDB)

    Windows10制作LMDB详细教程 原创不易,转载请注明出处:https://www.cnblogs.com/xiaoboge/p/10678658.html 摘要: 当我们在使用Caffe做深度 ...

  2. 性能分析神器VisualVM【转】

    性能分析神器VisualVM[转] Posted on 2015-04-17 09:37 WadeXu 阅读(5809) 评论(6) 编辑 收藏 VisualVM 是一款免费的,集成了多个 JDK 命 ...

  3. Nginx的安装--------tar包安装

    Nginx的安装,在网上搜索是很多的结果,但是 真的安装起来却花费了不少 心思,总结起来就是依赖包安装了,但是没有指定对的路径,在安装的过程中遇到过两个问题: ①make[1]: *** [/usr/ ...

  4. GROUP方法也是连贯操作方法之一

    GROUP方法也是连贯操作方法之一,通常用于结合合计函数,根据一个或多个列对结果集进行分组 . group方法只有一个参数,并且只能使用字符串. 例如,我们都查询结果按照用户id进行分组统计: $th ...

  5. springboot与热部署

    在开发中我们修改一个Java文件后想看到效果不得不重启应用,这导致大量时间花费,我们希望不重启应用的情况下,程序可以自动部署(热部署).有以下四种情况,如何能实现热部署. 1.模板引擎: 在Sprin ...

  6. markdown常用知识点

    为什么要用markdown写开发文档? 1.可以在git上在线预览,docx文档需要下载才能看见: 2. .md文档每次修改之后能被git管理,可追踪修改内容和修改人,但是docx不能追踪修改内容. ...

  7. 洛谷P4550 【收集邮票】

    题目链接: 神仙题QAQ 题目分析: 概率期望题是不可能会的,一辈子都不可能会的QAQ 这个题也太仙了 首先明确一下题意里面我感觉没太说清楚的地方,这里是抽到第\(i\)次要\(i\)元钱,不是抽到第 ...

  8. MyEclipse使用总结——在MyEclipse中新建Maven框架的web项目[转]

    前面的文章我们已经在本机安装好了maven,同时在myeclipse中配置好了maven的插件. 链接如下: Maven安装----在Windows上安装Maven myeclipse安装maven插 ...

  9. 安装Tengine和Tengine说明

    什么是Tengine 官方帮助文档:http://tengine.taobao.org/nginx_docs/cn/   Tengine的安装   新建tengine用户组 groupadd -r n ...

  10. Android基础控件EditText

    1.常用属性 <!--selectAllOnFocus 获得焦点后全选组件内所有文本内容--> <!--inputType 限制输入方式--> <!--singleLin ...