题目传送门

题意:训练指南P225

分析:二分寻找长度,用hash值来比较长度为L的字串是否相等。

#include <bits/stdc++.h>
using namespace std;typedef unsigned long long ull;
const int N = 4e4 + 5;
const int x = 123;
ull H[N], _hash[N], xp[N];
int rk[N];
char str[N];
int m;void get_hash(char *s, int len)  {H[len] = 0;for (int i=len-1; i>=0; --i) {H[i] = H[i+1] * x + (s[i] - 'a');}xp[0] = 1;for (int i=1; i<len; ++i) {xp[i] = xp[i-1] * x;}
}bool cmp(const int &a, const int &b) {return (_hash[a] < _hash[b] || (_hash[a] == _hash[b] && a < b));
}int check(int L, int len)    {int cnt = 0, pos = -1, c = 0;for (int i=0; i<len-L+1; ++i)   {rk[i] = i;_hash[i] = H[i] - H[i+L] * xp[L];}sort (rk, rk+len-L+1, cmp);for (int i=0; i<len-L+1; ++i)   {if (i == 0 || _hash[rk[i]] != _hash[rk[i-1]])   c = 0;if (++c >= m)   pos = max (pos, rk[i]);}return pos;
}int main(void)  {while (scanf ("%d", &m) == 1)   {if (!m) break;scanf ("%s", &str);int len = strlen (str);get_hash (str, len);if (check (1, len) == -1) puts ("none");else    {int l = 1, r = len + 1;while (r - l > 1)   {int mid = l + r >> 1;if (check (mid, len) >= 0)    l = mid;else    r = mid;}printf ("%d %d\n", l, check (l, len));}}return 0;
}

后缀数组也可以求解,具体就是二分答案,height数组分组判断是否满足存在题意的解,并使最优。(m=1时特判处理)

#include <bits/stdc++.h>const int N = 4e4 + 5;
int sa[N], rank[N], height[N];
int ws[N], wa[N], wb[N];
char s[N];bool cmp(int *r, int a, int b, int l) {return (r[a] == r[b] && r[a+l] == r[b+l]);
}
void DA(char *r, int n, int m = 128) {int i, j, p, *x = wa, *y = wb;for (i=0; i<m; ++i) ws[i] = 0;for (i=0; i<n; ++i) ws[x[i]=r[i]]++;for (i=1; i<m; ++i) ws[i] += ws[i-1];for (i=n-1; i>=0; --i) sa[--ws[x[i]]] = i;for (j=1, p=1; p<n; j<<=1, m=p) {for (p=0, i=n-j; i<n; ++i) y[p++] = i;for (i=0; i<n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;for (i=0; i<m; ++i) ws[i] = 0;for (i=0; i<n; ++i) ws[x[y[i]]]++;for (i=1; i<m; ++i) ws[i] += ws[i-1];for (i=n-1; i>=0; --i) sa[--ws[x[y[i]]]] = y[i];std::swap (x, y);for (p=1, x[sa[0]]=0, i=1; i<n; ++i) {x[sa[i]] = cmp (y, sa[i-1], sa[i], j) ? p - 1 : p++;}}
}
void calc_height(char *r, int *sa, int n) {int i, j, k = 0;for (i=1; i<=n; ++i) rank[sa[i]] = i;for (i=0; i<n; ++i) {if (k) k--;j = sa[rank[i]-1];while (r[i+k] == r[j+k]) k++;height[rank[i]] = k;}
}int m;
int check(int len, int n) {int p = -1;int cnt = 0, ret = -1;for (int i=1; i<=n; ++i) {if (height[i] >= len) {if (p == -1) {p = std::max (sa[i-1], sa[i]);} else {p = std::max (p, std::max (sa[i-1], sa[i]));}cnt++;if (cnt + 1 >= m) {ret = std::max (ret, p);}} else {p = -1;cnt = 0;}}return ret;
}int main() {while (scanf ("%d", &m) == 1) {if (!m) break;scanf ("%s", s);int n = strlen (s);if (m == 1) {printf ("%d %d\n", n, 0);continue;}DA (s, n + 1);calc_height (s, sa, n);int best = 0, pos = -1;int left = 0, right = n;while (left <= right) {int mid = left + right >> 1;int res = check (mid, n);if (res != -1) {if (best < mid) {best = mid;pos = res;} else if (mid > 0 && best == mid && pos < res) {pos = res;}left = mid + 1;} else {right = mid - 1;}}if (pos == -1) {puts ("none");} else {printf ("%d %d\n", best, pos);}}return 0;
}

  

转载于:https://www.cnblogs.com/Running-Time/p/5123779.html

Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens相关推荐

  1. LA 4513 Stammering Aliens

    在这道题上学会了两种方法解决.一种是用哈希lcp法,另一种用后缀数组求解. Dr. Ellie Arroway has established contact with an extraterrest ...

  2. FJUT3703 这还是一道数论题(二分 + hash + manacher 或者 STL + hash 或者 后缀数组 + hash)题解...

    Problem Description 最后来个字符串签个到吧,这题其实并不难,所需的算法比较基础,甚至你们最近还上过课. 为了降低难度,免得所有人爆零.这里给几个提示的关键字 :字符串,回文,二分, ...

  3. UVALive 4513 Stammering Aliens

    题意: 给定一个数m,一个字符串s,求s中至少出现m次的最长子串长度,并输出位置最靠后的对应子串的下标:串长度<=40000 分析: 构造后缀数组: 先二分答案mid,然后检查高度数组(LCP) ...

  4. 算法竞赛进阶指南---0x14(Hash)后缀数组

    题面 题解 我们先来看朴素做法,对于一个长度为n的字符串,它的后缀也有n个,将他们排序(用快排)是 O(nlogn) ,如果是暴力比较两个字符串的字典序是 O(n) ,那么总的时间复杂度就是O(n2l ...

  5. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)

    以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...

  6. cf244D. Match amp; Catch 字符串hash (模板)或 后缀数组。。。

    D. Match & Catch 能够用各种方法做.字符串hash.后缀数组,dp.拓展kmp,字典树.. . 字符串hash(模板) http://blog.csdn.net/gdujian ...

  7. 看动画学算法系列之:后缀数组suffix array

    文章目录 简介 后缀数组的定义 后缀数组的创建流程 在后缀数组中查找某个字符串 创建LCP 后缀数组和后缀树的比较 简介 在之前的文章中,我们讲到了后缀树和它的一些特性.后缀树主要用来做模式匹配中,比 ...

  8. 后缀数组 + Hash + 二分 or Hash + 二分 + 双指针 求 LCP ---- 2017icpc 青岛 J Suffix (假题!!)

    题目链接 题目大意: 就是给你n个串每个串取一个后缀,要求把串拼起来要求字典序最小!! sum_length_of_n≤5e5sum\_length\_of\_n\leq 5e5sum_length_ ...

  9. HDU4080 Stammering Aliens(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4080 Description Dr. Ellie Arroway has establish ...

最新文章

  1. php嵌入html还是html嵌入php,php嵌入html有哪几种方法
  2. Matlab 线性规划问题模型代码
  3. 淘淘商城学习笔记 之 上传图片到远程服务器,图片的回显出现的bug
  4. JavaScript(js)概述和使用
  5. 在线教育、直播教育、课程直播、订单系统、老师介绍、收入提现、在线学习、业绩统计、课程统计、选老师、选课程、作业管理、课程管理、报名统计、在线教育管理系统、axure原型、rp源文件
  6. lnmp 虚拟主机的配置
  7. Python:如何安装whl文件
  8. step-by-step: 夕小瑶版神经网络调参指南
  9. Jmeter(四十七)_性能测试统计超时率
  10. (3)fastjson带有转义字符的数据格式处理
  11. 【转贴】从亚马逊公司的发展看电子商务
  12. CheckBox和ListView的结合使用
  13. 42招健脑秘笈——必看
  14. 导出到excel,如果不保存,会自动关闭页面的问题
  15. 本科项目——51单片机多功能万年历
  16. HuaWei ❀ Virtual Firewalld 虚拟防火墙
  17. JavaScript制作简易聊天窗口
  18. 【JS基础】立即执行函数表达式(自执行函数)
  19. Anaconda超详细下载安装配置教程(Windows)
  20. nginx服务器的文件大小的限制解决办法

热门文章

  1. php导出excel出现乱码,php导出数据到excel出现乱码的解决办法
  2. 【Ogre-windows】环境配置
  3. elasticsearch的插件安装
  4. .NET开发过程中的全文索引使用技巧之Solr
  5. 1.1.1 从简单的数据类型开始
  6. 开发工具MyEclipse如何支持可视化设计HTML和JSP页面
  7. iOS UITextField使用全攻略
  8. java变量及进制问题 —(4)
  9. 记录一次内网渗透试验
  10. 关于__getattribute__