题目传送门

题意:训练指南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. Sql如何自动定时备份数据库

    直接上图

  2. [Android Pro] Service (startservice , bindservice , unbindservice, stopService)

    1: startService -------stopService (this will call onDestroy) 2: bindService -------unbindService    ...

  3. HttpClient 3.X 4.3 4.x超时设置

    HttpClient 4.3.HttpClient这货和Lucene一样,每个版本的API都变化很大,这有点让人头疼.就好比创建一个HttpClient对象吧,每一个版本的都不一样, 3.X是这样的 ...

  4. DOM对象与JQUERY对象的相互转化

    普通处理,通过标准JavaScript处理: 1 var p = document.getElementById('imooc') 2 p.innerHTML = '您好!学习jQuery才是最佳的途 ...

  5. Eclipse 安装SVN

    地址:http://wenku.baidu.com/link?url=ntQy2-1CjlNyUpO0-4uhROrc9jCo12Yifh7MkPULmY_dCybl6SEH99SxYxEbZQEiW ...

  6. 根据复选框checkbox的选中状态来打开或关闭隐藏层

    HTML:  <input type="checkbox" id="check-expert"> <div id="expert&q ...

  7. mysql 查看用户的权限

    show grants for 'username'@'%';

  8. ActiveMQ的几种集群配置

    ActiveMQ是一款功能强大的消息服务器,它支持许多种开发语言,例如Java, C, C++, C#等等.企业级消息服务器无论对服务器稳定性还是速度,要求都很高,而ActiveMQ的分布式集群则能很 ...

  9. Babelfish(二分查找,字符串的处理略有难度,用sscanf输入)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28581   Accepted: 12326 题目链接: ...

  10. BI 项目管理之角色和职责

          DW/BI 系统在生命周期中需要许多不同的角色和技能,它们来自业务和技术领域.本文将介绍创建DW/BI 系统所涉及的主要角色.角色和人之间很少是一对一关系.与我们合作的团队小到只有一人,大 ...