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 不要写错了就行了 代码 ...
随机推荐
- 内核下枚举进程 (二)ZwQuerySystemInformation
说明: SYSTEM_INFORMATION_CLASS 的5号功能枚举进程信息.其是这个函数对应着ring3下的 NtQuerySystemInformation,但msdn上说win8以后ZwQu ...
- LoadRunner添加Weblogic监控的注意事项(非单纯的操作步骤)
LoadRunner添加Weblogic监控的注意事项(非单纯的操作步骤) 关于LR如何监控Weblogic(JMX方式)的操作就不在这里多说了,帮助文件和网上的介绍已经非常多了,关键是对各操作步 ...
- Ionic3 demo TallyBook 实例2
1.添加插件 2.相关页面 消费页面: <ion-header> <ion-navbar> <ion-title> 消费记录 </ion-title> ...
- JS高级特性
一.JavaScript的同源策略 参考链接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Same_origin_policy_fo ...
- Docker系列(十四):Kubernetes API和源码分析
Kubernetes API入门 Ku8 eye开源项目
- PAT甲级——A1078 Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- java日志管理 - slf4j+log4j2
1 . 概述 1.1 日志框架实现 log4j是apache实现的一个开源日志组件: logback同样是由log4j的作者设计完成的,拥有更好的特性,用来取代log4j的一个日志框架,是slf4j ...
- Spring Boot配置公共的线程池
内存资源很宝贵,线程池资源不宜过多的创建,同一个应用,尽量使用统一的线程池,并且相关参数需要设置适当,不造成资源的浪费,也不影响性能的提升. import java.util.concurrent.T ...
- 如何解决mysql服务器load高
.登录主机 # ssh hostname .确定是否是mysql导致 # top .查看是哪些sql正在慢查询 # mysql -h hostname -P port -u username # sh ...
- 我的js运动库新
1.一些样式的获取和设置 //通过id获取当前元素 //params:id function $id(id) { return document.getElementById(id); } //向cs ...