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:
1
17
b
a
b
b
a
b
a
a
b
a
a
b
a
a
b
a
b Output:
4

since a (4, 3)-repeat is found starting at the 5th character of the input string.

 

题目链接:SPOJ Repeats

论文里写的比较模糊,突然就往后匹配了,还往前匹配,完全没讲怎么匹配啊,代码还是看这个博客写的:传送门

说一下个人理解,为什么$LCP(i,i+L)/len+1$就是出现的次数?

首先对于一个由循环节构成的字符串$str$,假设它的长度为$len$,最小循环节长度为$k$,那么对于任意的$0 \le i \le len-1-k$,都有$str[i]==str[k+i]$

现在回到LCP问题上,假设两个串的公共前缀已知记为$lcp$,我们枚举的循环节长度为$L$,当前遍历位置为$i$,那么显然有$S[i+j]==S[i+L+j], 0 \le j \le lcp-1$,看这条式子,是不是跟上面的定义式子很像,显然有$len-1-k=lcp-1$,化简得$len=lcp+k$,因此仅仅往后推的循环次数是$(lcp+k)/k=lcp/k+1$,那么仅仅是往后推的最优解,那说不定前面刚好多了几个位置也是相同前缀,跟$lcp\%L$多出来的数凑一凑又是$L$呢?如果这样要至少补$lcp-lcp\%L$,因此我们枚举这个"至少"的位置$i-(lcp-lcp\%L)$,如果这个位置都可以和后面多余的补出一个$L$,那么往前也肯定是可以的,这里可能又回想,那干嘛不再往前考虑考虑,补出2个、3个、4个甚至更多的L呢,应该是没这个必要,因为假如你前面可以补更多的L,那么在前几次遍历的时候它早就被算进了往后推的$lcp$里了,不需要多往前考虑,当然全过程要注意下标是否合法,往前推到负数位置肯定是不行的。还有就是这个题一开始的答案一定要是1,因为1是肯定可以的,因此我们是从$L=2$开始枚举

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 50010;
int wa[N], wb[N], cnt[N], sa[N];
int ran[N], height[N];
char s[N]; inline int cmp(int r[], int a, int b, int d)
{
return r[a] == r[b] && r[a + d] == r[b + d];
}
void DA(int n, int m)
{
int i;
int *x = wa, *y = wb;
for (i = 0; i < m; ++i)
cnt[i] = 0;
for (i = 0; i < n; ++i)
++cnt[x[i] = s[i]];
for (i = 1; i < m; ++i)
cnt[i] += cnt[i - 1];
for (i = n - 1; i >= 0; --i)
sa[--cnt[x[i]]] = i;
for (int k = 1; k <= n; k <<= 1)
{
int p = 0;
for (i = n - k; i < n; ++i)
y[p++] = i;
for (i = 0; i < n; ++i)
if (sa[i] >= k)
y[p++] = sa[i] - k;
for (i = 0; i < m; ++i)
cnt[i] = 0;
for (i = 0; i < n; ++i)
++cnt[x[y[i]]];
for (i = 1; i < m; ++i)
cnt[i] += cnt[i - 1];
for (i = n - 1; i >= 0; --i)
sa[--cnt[x[y[i]]]] = y[i];
swap(x, y);
x[sa[0]] = 0;
p = 1;
for (i = 1; i < n; ++i)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], k) ? p - 1 : p++;
m = p;
if (m >= n)
break;
}
}
void gethgt(int n)
{
int i, k = 0;
for (i = 1; i <= n; ++i)
ran[sa[i]] = i;
for (i = 0; i < n; ++i)
{
if (k)
--k;
int j = sa[ran[i] - 1];
while (s[j + k] == s[i + k])
++k;
height[ran[i]] = k;
}
}
namespace SG
{
int dp[N][17];
void init(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
dp[i][0] = height[i];
for (j = 1; l + (1 << j) - 1 <= r; ++j)
{
for (i = l; i + (1 << j) - 1 <= r; ++i)
dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
}
int ask(int l, int r)
{
int len = r - l + 1;
int k = 0;
while (1 << (k + 1) <= len)
++k;
return min(dp[l][k], dp[r - (1 << k) + 1][k]);
}
int LCP(int l, int r, int len)
{
l = ran[l], r = ran[r];
if (l > r)
swap(l, r);
if (l == r)
return len - sa[l];
return ask(l + 1, r);
}
}
int main(void)
{
int T, len, i;
scanf("%d", &T);
while (T--)
{
scanf("%d", &len);
for (i = 0; i < len; ++i)
scanf("%s", s + i);
DA(len + 1, 130);
gethgt(len);
SG::init(1, len);
int ans = 1;
for (int L = 1; L < len; ++L)
{
for (i = 0; i + L < len; i += L)
{
int lcp = SG::LCP(i, i + L, len);
int cnt = lcp / L + 1;
int j = i - (L - lcp % L);
if (j >= 0 && lcp % L != 0 && SG::LCP(j, j + L, len) / L + 1 > cnt)
++cnt;
ans = max(ans, cnt);
}
}
printf("%d\n", ans);
}
return 0;
}

SPOJ Repeats(后缀数组+RMQ-ST)的更多相关文章

  1. spoj687 REPEATS - Repeats (后缀数组+rmq)

    A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...

  2. SPOJ REPEATS 后缀数组

    题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...

  3. SPOJ - REPEATS Repeats (后缀数组+RMQ)

    题意:求一个串中出现重复子串次数最多的数目. 析:枚举每个长度的子串,至少要重复两次,必然会经过s[l*i]中相邻的两个,然后再分别向前和向后匹配即可. 代码如下: #pragma comment(l ...

  4. SPOJ - REPEATS —— 后缀数组 重复次数最多的连续重复子串

    题目链接:https://vjudge.net/problem/SPOJ-REPEATS REPEATS - Repeats no tags  A string s is called an (k,l ...

  5. SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解

    题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[ ...

  6. POJ 3693 后缀数组+RMQ

    思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

  7. 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过

    题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...

  8. SPOJ - REPEATS Repeats (后缀数组)

    A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...

  9. HDU2459 后缀数组+RMQ

    题目大意: 在原串中找到一个拥有连续相同子串最多的那个子串 比如dababababc中的abababab有4个连续的ab,是最多的 如果有同样多的输出字典序最小的那个 这里用后缀数组解决问题: 枚举连 ...

  10. hdu 2459 (后缀数组+RMQ)

    题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...

随机推荐

  1. js 节点

    var chils= s.childNodes; //得到s的全部子节点 var par=s.parentNode;  //得到s的父节点 var ns=s.nextSbiling;  //获得s的下 ...

  2. 【php】php 生僻知识点认知

    资料引用来源:http://www.runoob.com/php/php-tutorial.html ​ 表单提交中, 变量名中的点和空格被转换成下划线.例如 <input name=" ...

  3. python学习之面向对象程序设计的一些思考

    将属于一类的对象放在一起: 如果一个函数操纵一个全局变量,那么两者最好都在类内作为特性和方法实现. 不要让对象过于亲密: 方法应该只关心自己实例的特性,让其他实例管理自己的状态. 简单就好: 让方法小 ...

  4. 记一次MD5妙用

    记一次MD5妙用 最近项目组中在做历史记录的改造工作,主持讨论了多次,但每次讨论完都觉的很完美了,但实际在写这部分逻辑的时候还是会发现一些问题出来,很难受,反反复复的暴露智商是硬伤,人艰不拆,暂先不扯 ...

  5. ABS(引数と同じ大きさの正の数を返す)

    ABS 関数 [数値] 数値式の絶対値を返します. 構文 ABS( numeric-expression ) パラメータ numeric-expression   絶対値が返される数値. 戻り値 数値 ...

  6. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  7. Close Java Auto Update in Windows 7 and Later

    0. Environment Windows 7JDK 1.6.0_45 1. Steps 1) Enter "JRE/bin" 2) Run javacpl.exe as adm ...

  8. Windows自带的磁盘填充命令

    一张不用了的SD卡要给别人,之前一直是手机使用的,担心有一些资料被恢复,想要将它内容清空.以前就知道数字公司有一个磁盘填充的工具,后来网上搜一搜发现Windows有一个自带的命令用于磁盘填充. 首先进 ...

  9. 国际电话区号SQL

    CREATE TABLE `phone_prefix` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `country` varchar(30) N ...

  10. pytest 测试报告

    测试报告 运行测试用例后,为了保存结果,我们需要生成测试报告,同时可以把运行的测试报告发送相关人员查阅,这时需要安装一个插件(pytest-html) pytest-html插件安装 pip inst ...