spoj Repeats 后缀数组

来源:互联网 发布:mac上的养成游戏 编辑:程序博客网 时间:2024/05/21 09:53

REPEATS - Repeats

no tags 

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:117babbabaabaabaababOutput:4
since a (4, 3)-repeat is found starting at the 5th character of the input string.


题意:求字符串中连续重复字串的最大重复次数

首先重复一次的情况不同考虑。对于出现至少两次的情况,记这个字串为s,那么s一定包含字符r[0],r[len],r[2*len],r[3*len]...中的相邻两个字符串,
所以穷举len,再暴力每个i*len,求出lcp(i,i+len),那么当可以得到重复次数:lcp/len+1,但这并不一定是正确的,如果i位置是重复字串的首字符,这就是正确的,如果不是前面几个字符可能多构成一次重复,对此我们求出lcp(k=i-(len-lcp(i,i+len)%len),k+len),如果lcp>=lcp(i,i+len),则说明重复次数多一次,两两位置之间的lcp可用RMQ求得。



#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("%d\n", ans);}return 0;}




原创粉丝点击