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. TcpServer 使用简介

    1.简介 1) Poco 的 TcpServer 是一个多线程的 Tcp 服务器. 服务器使用 ServerSocket(Poco 的一个用于初始化服务器的socket的类) 来接收链接.Server ...

  2. Redis学习推荐

    Redis快速入门 https://www.yiibai.com/redis/redis_quick_guide.html Redis用途和使用场景 https://blog.csdn.net/wei ...

  3. maven-生命周期与插件

    Maven的生命周期是抽象的,具体的操作由插件实现,类似于java的模板设计模式. 1.生命周期 认识生命周期 maven有clean.default.site三种生命周期,每种生命周期都包含一些阶段 ...

  4. QP之QEP原理

    1.QP简介: 量子平台(Quantum Platform, 简称QP)是一个用于实时嵌入式系统的软件框架,QP是轻量级的.开源的.基于层次式状态机的.事件驱动的平台. QP包括事件处理器(QEP). ...

  5. dns文件

    1.dns简介 dns为域名解析系统,当本地浏览器输入域名访问网站时,如果本地host中没有配置域名与IP的对应关系,那么域名信息将会被发送到dns服务器上,由dns服务器将域名解析为IP(过程较为复 ...

  6. P1396 营救(最小瓶颈路)

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  7. Dubbo原理及配置

    技术交流群:233513714 Dubbo的背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进 ...

  8. 链接程序的时候遇到问题:fatal error LNK1104: cannot open file 'rctrl-d.lib'

    1.lib库文件没有添加到工程中(工程里面根本就没有这个文件) 2.

  9. 最火的.NET开源项目[转]

    综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...

  10. jdk8 新特性stream().map()

    1.大写字符串列表 1.1 简单的Java示例将Strings列表转换为大写 TestJava8.java package com.mkyong.java8; import java.util.Arr ...