题目传送门

题意:训练指南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. [Android Pro] synchronized与static synchronized 的区别

    reference to :  http://www.cnblogs.com/shipengzhi/articles/2223100.html 1.synchronized与static synchr ...

  2. 简易qq对话框

    //本程序由QT5 creator编译可运行 //dialog.h 1 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> class ...

  3. 关于s:iterator 和s:if 的结合使用

    <s:iterator value="list" status="st"> <div class="sidebar-nav" ...

  4. python以指定宽度及格式输出字符串

  5. Python 实现发送、抄送邮件功能

    发送邮件 问题 在web.py中,如何发送邮件? 解法 在web.py中使用web.sendmail()发送邮件. web.sendmail('cookbook@webpy.org', 'user@e ...

  6. C#的lock关键字

    using System; using System.Threading; namespace Test { class Program { //一.Lock定义 //lock 关键字可以用来确保代码 ...

  7. Android bluetooth用户自定义数据

    default mac: [btif_core.c] btif_fetch_local_bdaddr() default device name: [btif_dm.c] btif_get_defau ...

  8. 【C#】 用Route进行URL重写

    在.NET Framework 4中,微软推出了Route机制.这种机制不仅在MVC中大量运用,在WebForm中也可以使用. 和Contex.RewritePath()一样,Route功能也是写在G ...

  9. 如何减少JS的全局变量污染

    A,唯一变量 B,闭包

  10. ytu 1059: 判别该年份是否闰年(水题,宏定义)

    1059: 判别该年份是否闰年 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 222  Solved: 139[Submit][Status][Web ...