Uva12206 Stammering Aliens 后缀数组&&Hash
Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all efforts to decode their messages have failed so far because, as luck would have it, they have stumbled upon a race of stuttering aliens! Her team has found out that, in every long enough message, the most important words appear repeated a certain number of times as a sequence of consecutive characters, even in the middle of other words. Furthermore, sometimes they use contractions in an obscure manner.
For example, if they need to say bab twice, they might just send the message babab, which has been abbreviated because the second b of the first word can be reused as the first b of the second one. Thus, the message contains possibly overlapping repetitions of the same words over and over again. As a result, Ellie turns to you, S.R. Hadden, for help in identifying the gist of the message. Given an integer m, and a string s, representing the message, your task is to find the longest substring of s that appears at least m times. For example, in the message baaaababababbababbab, the length-5 word babab is contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero). No substring appearing 3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears 11 times or more (see example 2).
In case there are several solutions, the substring with the rightmost occurrence is preferred (see example 3).
Input
The input contains several test cases. Each test case consists of a line with an integer m (m ≥ 1), the minimum number of repetitions, followed by a line containing a string s of length between m and 40 000, inclusive. All characters in s are lowercase characters from ‘a’ to ‘z’. The last test case is denoted by m = 0 and must not be processed.
Output
Print one line of output for each test case. If there is no solution, output ‘none’; otherwise, print two integers in a line, separated by a space. The first integer denotes the maximum length of a substring appearing at least m times; the second integer gives the rightmost possible starting position of such a substring.
Sample Input
3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0
Sample
Output
5 12
none
4 2
大白书原题,没事干用后缀数组实现一下
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = + ;
/*
const int x = 123;
typedef unsigned long long ull;
ull H[MAXN], xp[MAXN];
ull hash[MAXN];
int rank[MAXN];
char s[MAXN];
int n, m, pos;
int cmp(const int& a, const int& b) {
return hash[a] < hash[b] || (hash[a] == hash[b] && a < b);
}
int possible(int L) {
int c = 0;
pos = -1;
for(int i = 0; i < n - L + 1; ++i)
{
rank[i] = i;
hash[i] = H[i] - H[i + L] * xp[L];
}
sort(rank, rank + n - L + 1, cmp);
for(int i = 0; i < n - L + 1; ++i)
{
if(i == 0 || hash[ rank[i] ] != hash[ rank[i - 1] ]) c = 0;
if(++c >= m) pos = max(pos, rank[i]);
}
return pos >= 0;
}
int main()
{
freopen("in.txt", "r", stdin);
freopen("out2.txt", "w", stdout);
while(~scanf("%d", &m) && m)
{
scanf("%s", s);
n = strlen(s);
H[n] = 0;
for(int i = n - 1; i >= 0; --i) H[i] = H[i + 1] * x + (s[i] - 'a');
xp[0] = 1;
for(int i = 1; i <= n; ++i) xp[i] = xp[i - 1] * x;
if(!possible(1)) puts("none");
else {
int L = 1, R = n + 1;
while(R - L > 1)
{
int M = (L + R) >> 1;
if(possible(M)) L = M;
else R = M;
}
possible(L);
printf("%d %d\n", L, pos);
}
}
return 0;
}
Hash
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = + ;
int t1[maxn], t2[maxn], c[maxn];
bool cmp(int *r, int a, int b, int l) {
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(char str[], int sa[], int Rank[], int heigh[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i < m; ++i) c[i] = ;
for(i = ; i < n; ++i) c[ x[i] = str[i] ]++;
for(int i = ; i < m; ++i) c[i] += c[i - ];
for(int i = n - ; i >= ; --i) sa[--c[x[i]]] = i; for(int j = ; j <= n; j <<= )
{
p = ;
for(i = n - j; i < n; ++i) y[p++] = i;
for(i = ; i < n; ++i) if(sa[i] >= j) y[p++] = sa[i] - j; for(i = ; i < m; ++i) c[i] = ;
for(i = ; i < n; ++i) c[x[y[i]]]++;
for(i = ; i < m; ++i) c[i] += c[i - ];
for(i = n - ; i >= ; --i) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ; x[ sa[] ] = ;
for(i = ; i < n; ++i)
x[ sa[i] ] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
if(p >= n) break;
m = p;
}
int k = ;
n--;
for(i = ; i <= n; ++i) Rank[ sa[i] ] = i;
for(i = ; i < n; ++i) {
if(k) k--;
j = sa[Rank[i] - ];
while(str[i + k] == str[j + k]) k++;
heigh[ Rank[i] ] = k;
}
} int Rank[maxn], heigh[maxn], sa[maxn];
char s[maxn];
void out(int n) {
puts("Rank[]");
///Rank数组的有效范围是0~n-1, 值是1~n
for(int i = ; i <= n; ++i) printf("%d ", Rank[i]);
puts("sa[]");
///sa数组的有效范围是1~n,值是0~n-1
for(int i = ; i <= n; ++i) printf("%d ", sa[i]);
puts("heigh[]");
///heigh数组的有效范围是2~n
for(int i = ; i <= n; ++i) printf("%d ", heigh[i]);
}
int me;
bool check(int x, int n) {
int cnt = ;
for(int i = ; i <= n; ++i) {
if(heigh[i] >= x) {
cnt++;
}else {
cnt = ;
} if(cnt >= me) return true;
}
return false;
}
int getp(int x, int n) {
int cnt = , pos = -, tmp = -;
for(int i = ; i <= n; ++i) {
if(heigh[i] >= x) {
cnt++;
tmp = max(tmp, max(sa[i - ], sa[i]));
}else {
cnt = ;
tmp = -;
}
if(cnt >= me) pos = max(pos, tmp);
}
if(cnt >= me) pos = max(pos, tmp);
return pos;
}
void solve(int n) {
int l = , r = n + ;
while(r - l > ) {
int mid = (l + r) >> ;
if(check(mid, n)) l = mid;
else r = mid;
}
printf("%d %d\n", l, getp(l, n));
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out1.txt", "w", stdout);
while(scanf("%d", &me) == && me) {
scanf("%s", s);
int n = strlen(s);
da(s, sa, Rank, heigh, n, );
// out(n);
if(me == ) printf("%d %d\n", n, );
else if(check(, n) == false) puts("none");
else solve(n);
}
return ;
}
/*
3
vfskumskkjuoooqmuwunamayoclhpmexorddoimixgvxsukjlekpgmoganvmnfwqhgalvosjb
*/
Suffixarray
后缀数组实现的时候还是对height数组的应用,同样是二分出一个L后,我们可以通过扫描一遍height数组来看满足的L长子串存不存在。若在heigh数组中,存在i属于[l,r],使得
heigh[i] >= L且(r-l+1) >= m, 那么满足条件
Uva12206 Stammering Aliens 后缀数组&&Hash的更多相关文章
- UVA 12206 - Stammering Aliens(后缀数组)
UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组.搞出height数组后,利用二分去查找就可以 这 ...
- HDU-4622 Reincarnation 后缀数组 | Hash,维护和,扫描
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给一个字符串,询问某字串的不同字串的个数. 可以用后缀数组来解决,复杂度O(n).先求出倍 ...
- FJUT3703 这还是一道数论题(二分 + hash + manacher 或者 STL + hash 或者 后缀数组 + hash)题解
Problem Description 最后来个字符串签个到吧,这题其实并不难,所需的算法比较基础,甚至你们最近还上过课. 为了降低难度,免得所有人爆零.这里给几个提示的关键字 :字符串,回文,二分, ...
- 后缀数组 hash求LCP BZOJ 4310: 跳蚤
后缀数组的题博客里没放进去过..所以挖了一题写写 充实下博客 顺便留作板子.. 一个字符串S中 内容不同的子串 有 sigma{n-sa[i]+1-h[i]} (噢 这里的h[]就是大家熟知的he ...
- UVA12206 Stammering Aliens 【SAM 或 二分 + hash】
题意 求一个串中出现至少m次的子串的最大长度,对于最大长度,求出最大的左端点 题解 本来想练哈希的,没忍住就写了一个SAM SAM拿来做就很裸了 只要检查每个节点的right集合大小是否不小于m,然后 ...
- cogs2223. [SDOI2016 Round1] 生成魔咒(后缀数组 hash 二分 set
题意:对一个空串每次在后面加一个字符,问每加完一次得到的字符串有几个不同的子串. 思路:每个子串都是某个后缀的前缀,对于每个后缀求出他能贡献出之前没有出现过的前缀的个数,答案累加就行. 要求每个后缀的 ...
- UVA12206 Stammering Aliens
思路 可以二分答案+哈希 判断有没有那个长为L的串出现至少m次即可 代码 #include <cstdio> #include <cstring> #include <a ...
- UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)
题意:找一个出现了m次的最长子串,以及这时的最右的位置. hash的话代码还是比较好写的,,但是时间比SA多很多.. #include <stdio.h> #include <alg ...
- Hash(LCP) || 后缀数组 LA 4513 Stammering Aliens
题目传送门 题意:训练指南P225 分析:二分寻找长度,用hash值来比较长度为L的字串是否相等. #include <bits/stdc++.h> using namespace std ...
随机推荐
- 【文件】读取一个文件夹下所有的jpg图片
今天做视频处理的时候,发现给的视频是用jpg图片的形式给出的,名字的命名规律性不是很强.就想找一种通用的遍历文件夹下图片的方法. 开始在网上找到了下面这份代码,发现只能读取所有的文件夹,文件都被跳过了 ...
- (2016弱校联盟十一专场10.5) F. Fibonacci of Fibonacci
题目链接 题目大意就是这个,先找出下标的循环节,再快速幂对20160519取余就行了. 找出下标循环节: #include <cstdio> #include <iostream&g ...
- 关于Javascript splice方法的一个坑。
w3c相关文档:http://www.w3school.com.cn/jsref/jsref_splice.asp bug:购物车计算价格的时候.加商品没问题,减商品的时候价格总是计算错误. 经排查发 ...
- IOS- 02 零碎知识总结
1.UIView,UIViewController,UIWindow和CALayer UIView是什么,做什么:UIView是用来显示内容的,可以处理用户事件 CALayer是什么,做什么:CALa ...
- php数据访问基础
建一个连接(造一个连接对象) $db = new MySqli("IP地址或者域名,若果是本地则用localhost","用户名","数据库密码&qu ...
- NYOJ题目889求距离
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsYAAAJ2CAIAAADTwNOXAAAgAElEQVR4nO3dPVLrSteG4W8S5B4IsQ
- 三、jQuery--jQuery基础--jQuery基础课程--第6章 jQuery 事件与应用
1.页面加载时触发ready()事件 ready()事件类似于onLoad()事件,但前者只要页面的DOM结构加载后便触发,而后者必须在页面全部元素加载成功才触发,ready()可以写多个,按顺序执行 ...
- Java程序员要求具备的10项技能
1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...
- oracle JOB学习(一)---基础
oracle job简介 下面文章来自网友(格式稍加整理) 主要的使用情景 定时在后台执行相关操作:如每天晚上0点将一张表的数据保存到另一张表中,2:定时备份数据库等 熟化说万事开头难,这 ...
- VS2013 当前不会命中断点,还没有为该文档加载任何符号
方法一: 把ie的 调试 打开,然后调试的时候 会问你 是在新示例中打开 还是 当前示例,你选择当前的就行了.还有 建议你用 ie8.0的 开发者工具 调试 非常舒服 我已经 早就不用debuger ...