概述
REPEATS - Repeats
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed string t with length l>=1. For example, the string
s = abaabaabaaba
is a (4,3)-repeat with t = aba as its seed string. That is, the seed string t is 3 characters long, and the whole string s is obtained by repeating t 4 times.
Write a program for the following task: Your program is given a long string u consisting of characters ‘a’ and/or ‘b’ as input. Your program must find some (k,l)-repeat that occurs as substring within u with k as large as possible. For example, the input string
u = babbabaabaabaabab
contains the underlined (4,3)-repeat s starting at position 5. Since u contains no other contiguous substring with more than 4 repeats, your program must output the maximum k.
Input
In the first line of the input contains H- the number of test cases (H <= 20). H test cases follow. First line of each test cases is n - length of the input string (n <= 50000), The next n lines contain the input string, one character (either ‘a’ or ‘b’) per line, in order.
Output
For each test cases, you should write exactly one interger k in a line - the repeat count that is maximized.
Example
Input: 1 17 b a b b a b a a b a a b a a b a b Output: 4since a (4, 3)-repeat is found starting at the 5th character of the input string.
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxm = 50005;
int a[maxm], s[maxm], Height[maxm], tp[maxm], sa[maxm], Rank[maxm], dp[maxm][20];
int n, m;
char str[maxm];
int cmp(int *f, int x, int y, int w)
{
return f[x] == f[y] && f[x + w] == f[y + w];
}
void Rsort()
{
for (int i = 0;i <= m;i++) s[i] = 0;
for (int i = 1;i <= n;i++) s[Rank[tp[i]]]++;
for (int i = 1;i <= m;i++) s[i] += s[i - 1];
for (int i = n;i >= 1;i--) sa[s[Rank[tp[i]]]--] = tp[i];
}
void suffix()
{
for (int i = 1;i <= n;i++) Rank[i] = a[i], tp[i] = i;
m = 30, Rsort();
for (int i, w = 1, p = 1;p < n;w += w, m = p)
{
for (p = 0, i = n - w + 1;i <= n;i++) tp[++p] = i;
for (i = 1;i <= n;i++) if (sa[i] > w) tp[++p] = sa[i] - w;
Rsort(), swap(Rank, tp), Rank[sa[1]] = p = 1;
for (i = 2;i <= n;i++) Rank[sa[i]] = cmp(tp, sa[i], sa[i - 1], w) ? p : ++p;
}
int j, k = 0;
for (int i = 1;i <= n;Height[Rank[i++]] = k, tp[i] = 0)
for (k = k ? k - 1 : k, j = sa[Rank[i] - 1];a[i + k] == a[j + k];k++);
}
void RMQ()
{
for (int i = 1;i <= n;i++) dp[i][0] = Height[i];
for (int j = 1;(1 << j) <= n;j++)
for (int i = 1;i + (1 << j) - 1 <= n;i++)
dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
int query(int x, int y)
{
x = Rank[x], y = Rank[y];
if (x > y) swap(x, y);
int k = 0;
while (1 << (k + 1) <= y - x) k++;
return min(dp[x + 1][k], dp[y - (1 << k) + 1][k]);
}
int main()
{
int i, j, k, sum, t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
getchar();
for (i = 1;i <= n;i++)
{
scanf("%c", &str[i]);
a[i] = str[i] - 'a' + 1;
getchar();
}
a[n + 1] = 0;
suffix(), RMQ();
int ans = 0, lcp, temp;
for (int len = 1;len <= n;len++)
{
for (int i = 0;i + len <= n;i += len)
{
lcp = query(i, i + len);
temp = lcp / len + 1;
k = i - (len - lcp%len);
if (k >= 0 && lcp%len)
if (query(k, k + len) >= lcp) temp++;
ans = max(ans, temp);
}
}
printf("%dn", ans);
}
return 0;
}
最后
以上就是忧心手机为你收集整理的spoj Repeats 后缀数组的全部内容,希望文章能够帮你解决spoj Repeats 后缀数组所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复