我是靠谱客的博主 无辜楼房,最近开发中收集的这篇文章主要介绍SPOJ REPEATS(后缀数组),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题意:给出一个字符串,问重复次数最多的子串的重复次数。
题解:和之前做过的poj3693很像http://blog.csdn.net/hyczms/article/details/49294095,不过这道题直接输出次数。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb))
#define G(x) ((x) < tb ? (x) * 3 + 1 : ((x) - tb) * 3 + 2)
using namespace std;
const int N = 50005;
int wa[N], wb[N], ws[N], wv[N], sa[N * 3];
int rank[N * 3], height[N * 3], s[N], f[N * 3][35];
char str[3];
int c0(int *r, int a, int b) {
return r[a] == r[b] && r[a + 1] == r[b + 1] && r[a + 2] == r[b + 2];
}
int c12(int k, int *r, int a, int b) {
if (k == 2)
return r[a] < r[b] || r[a] == r[b] && c12(1, r, a + 1, b + 1);
return r[a] < r[b] || r[a] == r[b] && wv[a + 1] < wv[b + 1];
}
void sort(int *r, int *a, int *b, int n, int m) {
for (int i = 0; i < n; i++) wv[i] = r[a[i]];
for (int i = 0; i < m; i++) ws[i] = 0;
for (int i = 0; i < n; i++) ws[wv[i]]++;
for (int i = 1; i < m; i++) ws[i] += ws[i - 1];
for (int i = n - 1; i >= 0; i--) b[--ws[wv[i]]] = a[i];
}
void dc3(int *r, int *sa, int n, int m) {
int i, j, *rn = r + n, *san = sa + n, ta = 0, tb = (n + 1) / 3, tbc = 0, p;
r[n] = r[n + 1] = 0;
for (i = 0; i < n; i++) if (i % 3 != 0) wa[tbc++] = i;
sort(r + 2, wa, wb, tbc, m);
sort(r + 1, wb, wa, tbc, m);
sort(r, wa, wb, tbc, m);
for (p = 1, rn[F(wb[0])] = 0, i = 1; i < tbc; i++)
rn[F(wb[i])] = c0(r, wb[i - 1], wb[i]) ? p - 1 : p++;
if (p < tbc) dc3(rn, san, tbc, p);
else for (i = 0; i < tbc; i++) san[rn[i]] = i;
for (i = 0; i < tbc; i++) if (san[i] < tb) wb[ta++] = san[i] * 3;
if (n % 3 == 1) wb[ta++] = n - 1;
sort(r, wb, wa, ta, m);
for (i = 0; i < tbc; i++) wv[wb[i] = G(san[i])] = i;
for (i = 0, j = 0, p = 0; i < ta && j < tbc; p++)
sa[p] = c12(wb[j] % 3, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
for (; i < ta; p++) sa[p] = wa[i++];
for (; j < tbc; p++) sa[p] = wb[j++];
}
void calheight(int *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; height[rank[i++]] = k)
for (k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
}
void RMQ_init(int cnt) {
for (int i = 0; i < cnt; i++)
f[i][0] = height[i];
for (int j = 1; (1 << j) <= cnt; j++)
for (int i = 0; i + (1 << j) - 1 < cnt; i++)
f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
int RMQ(int L, int R) {
int k = 0;
while (1 << (k + 1) <= R - L + 1) k++;
return min(f[L][k], f[R - (1 << k) + 1][k]);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int len;
scanf("%d", &len);
for (int i = 0; i < len; i++) {
scanf("%s", str);
s[i] = str[0] - 'a' + 1;
}
s[len] = 0;
dc3(s, sa, len + 1, 50);
calheight(s, sa, len);
RMQ_init(len + 1);
int maxx = 0;
for (int i = 1; i <= len / 2; i++) {
for (int j = 0; j + i < len; j += i) {
int pos1 = rank[j], pos2 = rank[j + i];
if (pos1 > pos2) swap(pos1, pos2);
int temp = RMQ(pos1 + 1, pos2);
int num = temp / i + 1;
int r = i - temp % i;
int pos = j, cnt = 0;
for (int k = j - 1; k > j - i && s[k] == s[k + i]; k--) {
cnt++;
if (cnt == r)
num++;
}
maxx = max(maxx, num);
}
}
printf("%dn", maxx);
}
return 0;
}

最后

以上就是无辜楼房为你收集整理的SPOJ REPEATS(后缀数组)的全部内容,希望文章能够帮你解决SPOJ REPEATS(后缀数组)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部