hdu1403 赤裸裸的后缀数组
赤裸裸的后缀数组的应用啊。直接将两个串连起来(中间加一个比小写字母小的字符,我学大家,用的‘#’),求出height数组,找最大值即可(得排除两个后缀在同一端的情况)
/** hdu1403/win.cpp* Created on: 2013-5-20* Author : ben*/ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <queue> #include <set> #include <map> #include <stack> #include <string> #include <vector> #include <deque> #include <list> #include <functional> #include <numeric> #include <cctype> using namespace std; const int MAXN = 200010; // MAXN > 256 char s[MAXN]; int sa[MAXN], height[MAXN], rank[MAXN], h[MAXN]; int tmp[MAXN], top[MAXN]; int N; void makesa() { // O(N * log N)int i, j, len, na;na = (N < 256 ? 256 : N);memset(top, 0, na * sizeof(int));for (i = 0; i < N; i++)top[rank[i] = s[i] & 0xff]++;for (i = 1; i < na; i++)top[i] += top[i - 1];for (i = 0; i < N; i++)sa[--top[rank[i]]] = i;for (len = 1; len < N; len <<= 1) {for (i = 0; i < N; i++) {j = sa[i] - len;if (j < 0)j += N;tmp[top[rank[j]]++] = j;}sa[tmp[top[0] = 0]] = j = 0;for (i = 1; i < N; i++) {if (rank[tmp[i]] != rank[tmp[i - 1]]|| rank[tmp[i] + len] != rank[tmp[i - 1] + len])top[++j] = i;sa[tmp[i]] = j;}memcpy(rank, sa, N * sizeof(int));memcpy(sa, tmp, N * sizeof(int));if (j >= N - 1)break;} }void lcp() { // O(4 * N)int i, j, k;for (j = rank[height[i = k = 0] = 0]; i < N - 1; i++, k++)while (k >= 0 && s[i] != s[sa[j - 1] + k])height[j] = (k--), j = rank[sa[j] + 1];for(int i = 0; i < N - 1; i++) {h[i] = height[rank[i]];} }char str[MAXN];inline bool judge(int x, int l1) {int i = sa[x];int j = sa[x - 1];return (i > l1) xor (j > l1); }int main() { #ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin); #endifwhile(scanf("%s%s", s, str) == 2) {int l1 = strlen(s);s[l1] = '#';s[l1 + 1] = 0;strcat(s, str);N = strlen(s) + 1;makesa();lcp();int ans = 0;for(int i = 1; i < N; i++) {if(height[i] > ans) {if(judge(i, l1)) {ans = height[i];}}}printf("%d\n", ans);}return 0; }
转载于:https://www.cnblogs.com/moonbay/archive/2013/05/20/3089567.html
hdu1403 赤裸裸的后缀数组相关推荐
- HDU1403(后缀数组--最长公共子串)
题目:Longest Common Substring 看代码注释请戳这里 题意:判断给定的两个串中,最长的公共串. 思路:将它们合并为一个串,然后利用后缀数组求解. 首先是二倍增算法:时间复杂度为O ...
- 后缀数组模板 hdu1403
题意:就是让你求两个字符串的最大子串 #include <bits/stdc++.h> const int maxn=200005; using namespace std; int s[ ...
- 字符串-后缀树和后缀数组详解
文章目录 后缀树 后缀数组 概念 sa[] rk[] height[] 例题 HDU-1403最长公共子串 洛谷P2408 不同子串个数 HDU-5769Substring 后缀树 建议先了解一下字典 ...
- 寻找一个字符串的重复子串 后缀数组
什么是后缀数组 令字符串 S=S[1]S[2]...S[n]S=S[1]S[2]...S[n]{\displaystyle S=S[1]S[2]...S[n]} , S[i,j]S[i,j]{\dis ...
- 【2012百度之星/资格赛】H:用户请求中的品牌 [后缀数组]
时间限制: 1000ms 内存限制: 65536kB 描述 馅饼同学是一个在百度工作,做用户请求(query)分析的同学,他在用户请求中经常会遇到一些很奇葩的词汇.在比方说"johnsonj ...
- Boring counting HDU - 3518 (后缀数组)
Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...
- HDU4080 Stammering Aliens(二分 + 后缀数组)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4080 Description Dr. Ellie Arroway has establish ...
- 后缀数组 + Hash + 二分 or Hash + 二分 + 双指针 求 LCP ---- 2017icpc 青岛 J Suffix (假题!!)
题目链接 题目大意: 就是给你n个串每个串取一个后缀,要求把串拼起来要求字典序最小!! sum_length_of_n≤5e5sum\_length\_of\_n\leq 5e5sum_length_ ...
- 后缀数组 ---- 2018~2019icpc焦作H题[后缀数组+st表+二分+单调栈]
题目链接 题目大意: 给出nnn个数,定义f[l,r]f[l,r]f[l,r]表示 区间[l,r][l,r][l,r]的最大值,求所有 子区间的最大值的和,要求相同的子区间只能算一次 比如数列 5 6 ...
- [Ahoi2013]差异[后缀数组+单调栈]
链接 解题思路:很明显前面∑1<=i<j<=nlen(Ti)+len(Tj)\sum_{1<=i<j<=n}len(T_i)+len(T_j)∑1<=i< ...
最新文章
- Android性能优化之一:ViewStub
- Ubuntu 16.04 LTS apt-get connect refuse Unable to connect to 127.0.0.1:1080
- python一年收入_你的年收入过5万了吗?数据科学家的Python模块和包
- zookeeper 屁民
- 交换机跟计算机系统有关系,网速跟交换机有关系吗
- 亚马逊发布新流媒体音乐服务:最低每月4美元
- Zookeeper C API 指南四(C API 概览)
- Filezilla server 使用教程
- lua学习03:tolua的编译和使用:C/C++调用lua、lua调用C++、lua调用tolua、常用tolua和lua的API介绍
- 大学计算机基础与计算思维习题,大学计算机基础与计算思维客观题及答案.doc...
- Faster RCNN原理篇(二)——RoIPooling和RoIAlign的学习和理解
- Mob免费验证码androidStudio实现
- 数字中国城市巡礼之开封:千年古都的智慧新生
- 小视频源码,设计模式单例模式
- 关于内存泄漏和内存溢出
- 广西投资集团推出“数字广投”,为广西数字化转型打版
- 自然语言语义相似度计算方法
- Git环境的配置及解决官方下载太慢的办法
- MAL-Gold Nanoparticle/Au nanoparticles纳米金颗粒/金纳米粒子|50nm浓度:0.5mg/ml
- 测试用例(QQ消息模块)