spoj Repeats 后缀数组
来源:互联网 发布:mac上的养成游戏 编辑:程序博客网 时间:2024/05/21 09:53
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:117babbabaabaabaababOutput: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("%d\n", ans);}return 0;}
- 【后缀数组】SPOJ REPEATS
- SPOJ REPEATS(后缀数组)
- SPOJ REPEATS 后缀数组
- spoj Repeats 后缀数组
- spoj 687 Repeats//后缀数组
- SPOJ 687. Repeats(后缀数组)
- SPOJ-REPEATS之后缀数组
- SPOJ 687 Repeats 后缀数组+暴力+rmq
- SPOJ 687 REPEATS Repeats 后缀数组 + RMQ预处理
- SPOJ 687. Repeats(后缀数组求最长重复子串)
- SPOJ SUBST1 POJ 2406 POJ REPEATS 后缀数组小结
- SPOJ REPEATS - Repeats(后缀数组[重复次数最多的连续重复子串])
- SPOJ687---REPEATS - Repeats(后缀数组+RMQ)
- spoj687 REPEATS - Repeats (后缀数组+rmq)
- [SPOJ687]REPEATS - Repeats(后缀数组+st)
- SPOJ 687 Repeats (后缀数组+RMQ 重复次数最多的连续重复子串)
- Repeats SPOJ (后缀数组,寻找一个重复次数最多的子串)
- Repeats SPOJ
- 边缘计算:万物互联新型计算模型
- AngularJS过滤器filter,orderBy排序,删除全部数据
- C++笔记——命名空间namespace
- Java面试题Day3
- r语言 偏度峰度
- spoj Repeats 后缀数组
- 基于kaldi、thchs30 的离线中文识别
- shadowsocks安装配置和使用
- linux命令--chgrp
- 睛睛爸爸的金蝶K/3 14.3 WISE
- YUV数据格式解析
- python yield函数深入浅出理解
- leetcode.array--56. Merge Intervals
- 快速排序