题目传送门

题意:训练指南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;
}

  

Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens的更多相关文章

  1. UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)

    题意:找一个出现了m次的最长子串,以及这时的最右的位置. hash的话代码还是比较好写的,,但是时间比SA多很多.. #include <stdio.h> #include <alg ...

  2. 140. 后缀数组(hash + 二分 / 后缀数组)

    题目链接 : https://www.acwing.com/problem/content/description/142/ Hash + 二分 #include <bits/stdc++.h& ...

  3. poj 2774 最长公共子--弦hash或后缀数组或后缀自己主动机

    http://poj.org/problem?id=2774 我想看看这里的后缀数组:http://blog.csdn.net/u011026968/article/details/22801015 ...

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

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

  5. BZOJ 2946 [Poi2000]公共串 (二分+Hash/二分+后缀数组/后缀自动机)

    求多串的最长公共字串. 法1: 二分长度+hash 传送门 法2: 二分+后缀数组 传送门 法3: 后缀自动机 拿第一个串建自动机,然后用其他串在上面匹配.每次求出SAM上每个节点的最长匹配长度后,再 ...

  6. uvalive 4513 Stammering Aliens

    题意:给你一个串,问期中至少出现m次的最长子串及其起始位置的坐标. 思路:hash+LCP+二分答案 #include<cstdio> #include<cstring> #i ...

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

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

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

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

  9. Uva12206 Stammering Aliens 后缀数组&&Hash

    Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...

随机推荐

  1. C#文件夹和文件操作

    File.Exist(string path)//文件读写FileStream fs=new FileStream(filename, FileMode.Create);BinaryWriter bw ...

  2. [Android Pro] proguard

    -optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassnames # 是否使用大小写混合 -dontskipnonpubliclibraryc ...

  3. August 11th 2016, Week 33rd Thursday

    A particular fine spring came around. 转眼又是一番分外明媚的春光. Hey, it is hot outside, sometimes even unbearab ...

  4. NYOJ题目34韩信点兵

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAHiCAIAAACV1MbSAAAgAElEQVR4nO3dPXLjONeG4W8TyrUQx1 ...

  5. c++序列化方法

    暂时使用boost 序列化, 目前我的测试基本都ok 只是对于c++11 shared ptr 没有测试成功,只能手工写下shared ptr 部分的序列化,也就是目前我对指针都不直接序列化,自己管理 ...

  6. mxnet环境搭建随记

    安装mxnet还是遇到不少麻烦事,现在简单记一下,挖个坑,后续详细补充,打算写一下我的mxnet探索之旅. 更新: 具体安装mxnet,cuda,opencv过程已经补坑——点击进入 不知道为什么,在 ...

  7. Task使用小结

    Task是.NET推出数据任务处理的工作类,Task的使用也被越来越多的人讲解,这里仅仅介绍Task的部分使用介绍: 1.Task简单创建 --无返回值 Task.Factory.StartNew(( ...

  8. SQL分组和聚合(Grouping and Aggregates)

    这章应该是难点,也是成为SQL高手的必经之路. 注意有GROUP 语句时,WHERE和HAVING的场合. 前者用于检索前的条件过滤 . 后者用于检索出来结果之后的条件过滤. ========== ; ...

  9. 恢复 git reset -hard 的误操作

    有时候使用Git工作得小心翼翼,特别是涉及到一些高级操作,例如 reset, rebase 和 merge.甚至一些很小的操作,例如删除一个分支,我都担心数据丢失. 不 久之前,我在做一些大动作(re ...

  10. Linux 6.5網卡配置

    Linux 6.5 網卡配置 路徑:/etc/sysconfig/network-scripts 1.關閉NetworkManager服務 [root@rhcsasm2 network-scripts ...