Hdu 1403(后缀数组)
Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4077 Accepted Submission(s): 1544Problem DescriptionGiven two strings, you have to tell the length of the Longest Common Substring of them.For example:
str1 = banana
str2 = cianaicSo the Longest Common Substring is "ana", and the length is 3.
InputThe 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.
OutputFor each test case, you have to tell the length of the Longest Common Substring of them.Sample Inputbanana
cianaicSample Output3
/*************************************************************************
> 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(后缀数组)的更多相关文章
- HDU - 1403 后缀数组初步
题意:求两个串的最长公共子串 两个串连接起来然后求高度数组 注意两个sa值必须分别在不同一侧 本题是用来测试模板的,回想起青岛那次翻车感觉很糟糕 #include<iostream> #i ...
- hdu 3948 后缀数组
The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- hihoCoder 1403 后缀数组一·重复旋律(后缀数组+单调队列)
#1403 : 后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成 ...
- HDU - 3948 后缀数组+Manacher
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...
- HDU 5769 后缀数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 [2016多校contest-4] 题意:给定一个字符,还有一个字符串,问这个字符串存在多少个不 ...
- hdu 2459 (后缀数组+RMQ)
题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...
- hdu 3518(后缀数组)
题意:容易理解... 分析:这是我做的后缀数组第一题,做这个题只需要知道后缀数组中height数组代表的是什么就差不多会做了,height[i]表示排名第i的后缀与排名第i-1的后缀的最长公共前缀,然 ...
- hdu 5442 (后缀数组)
稍微学习了下第一次用后缀数组- - , 强行凑出答案 , 感觉现在最大的问题是很多算法都不知道 ,导致有的题一点头绪都没有(就像本题). /*推荐 <后缀数组——处理字符串的有力工具>— ...
- HIHOcoder 1403 后缀数组一·重复旋律
思路 后缀数组的板子题,注意后缀数组的rank[]数组是通过位置找到对应排名的,sa[]是通过排名找到位置的,height[i]记录的是sa[i]和sa[i+1]之间的lcp 不要写错了就行了 代码 ...
随机推荐
- JavaScript特效源码(8、其他特效)
1.中文日期 中文日期[无须修改][共1步]] ====1.将以下代码加入HEML的<body></body>之间 <script LANGUAGE="Java ...
- differential related impedance and termination
Impedance (1) Z0 Z0 is the impedance of one T-line while other lines are held at 0. Single end. (2) ...
- 廖雪峰Java11多线程编程-3高级concurrent包-9Fork_Join
线程池可以高效执行大量小任务: Fork/Join线程池可以执行一种特殊的任务: 把一个大任务拆成多个小任务并行执行 Fork/Join是在JDK 1.7引入的 示例:计算一个大数组的和 Fork/J ...
- Java实现RSA加密
末尾贴上代码↓↓↓↓↓↓↓↓↓↓↓↓ 1.原理 2.实现过程 3. 公式 4.举例 p=13, q=11 , (p,q互质) N=p*q=143 L=(p-1)*(q-1)=120 E=7 ...
- csp-s模拟64trade,sum,building题解
题面:https://www.cnblogs.com/Juve/articles/11639755.html trade: 70分sbdp,然后一直想优化,dp还是很好写的 正解是反悔贪心 维护一个小 ...
- 连通图,set——cf1037E
看了题解又调了很久,用set来维护当前满足条件的pair<degree[v],v> 离线操作,先建好一张图,然后建立好集合,每次删边后都把集合里不满足条件的点删去,同时更新集合 /* 离线 ...
- 点击回退时需要点击2次才可返回js
为a加上window.location.href跳转页面时,再返回到此页面,再点击返回时需点击2次才能返回到前一个页面,原因竟然是href=“#”的原因,在html中#可做为锚点 http://blo ...
- hdu 1754 I Hate It (线段树)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树的模板题,详细的都写在代码里了 //不知道为什么定义单个字符,用%c输入会超时,换成字符数 ...
- 删除文件夹时提示“You need permission to perform this action。。。”,如何解决?
Win10系统,有时,要删除某个文件夹,却提示“You need permission to perform this action...” 以下是我Google之后找到的解决方案 1.创建一个文本文 ...
- VNC 4.25注册码
注册码:ELBMU-ZFYMV-2HC77-73M46-UL4TA97KLJ-VBTAL-T7GN2-K29PS-ANXCA45YV6-WXWMJ-NPAAV-HWD7Q-W5HVAL76HR-642 ...