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

题意:
求重复次数最多的连续重复子串。
 
思路:
可以先枚举重复子串的长度L,然后求长度为 L 的子串最多能连续出现几次。
假设在原字符串中连续出 现 2 次,记这个子字符串为 S,那么 S 肯定包括了字符 r[0], r[L], r[L*2],
r[L*3], ……中的某相邻的两个。所以只须看字符 r[L*i]和 r[L*(i+1)]往前和
往后各能匹配到多远,记这个总长度为 K,那么这里连续出现了 K/L+1 次。最后
看最大值是多少。
对于后面的那部分可以根据height[]直接求得。对于前面的那部分
先求后面部分减去长度L的x个子串后,是否还有。如果还有那么这一部分肯定属于前面。
这样L - (后面部分长度)%L就是前面部分的长度tp,然后判断 r[L* i] - tp 和 r[L* i] - tp + L的公共前缀,
如果长度大于等于r[L*i]和r[L*(i+1)]的公共长度,那么前面一定只是有1.
 
/*
* Author: sweat123
* Created Time: 2016/6/29 15:28:42
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int wa[MAXN],wb[MAXN],wc[MAXN],n,height[MAXN],Rank[MAXN],r[MAXN],sa[MAXN];
char s[MAXN];
void da(int *r,int *sa,int n,int m){
int *x = wa,*y = wb;
for(int i = ; i < m; i++)wc[i] = ;
for(int i = ; i < n; i++)wc[x[i] = r[i]] ++;
for(int i = ; i < m; i++)wc[i] += wc[i-];
for(int i = n - ; i >= ; i--)sa[--wc[x[i]]] = i;
for(int k = ,p = ; p < n; m = p,k <<= ){
p = ;
for(int i = n - k; i < n; i++)y[p++] = i;
for(int i = ; i < n; i++)if(sa[i] >= k)y[p++] = sa[i] - k;
for(int i = ; i < m; i++)wc[i] = ;
for(int i = ; i < n; i++)wc[x[y[i]]] ++;
for(int i = ; i < m; i++)wc[i] += wc[i-];
for(int i = n - ; i >= ; i--)sa[--wc[x[y[i]]]] = y[i];
swap(x,y);
p = ;
x[sa[]] = ;
for(int i = ; i < n; i++){
x[sa[i]] = (y[sa[i]] == y[sa[i-]] && y[sa[i]+k] == y[sa[i-]+k])?p-:p++;
}
}
}
void calheight(int *r,int *sa,int n){
for(int i = ; i <= n; i++)Rank[sa[i]] = i;
int j,k = ;
for(int i = ; i < n; height[Rank[i++]] = k){
for(k?k--:,j = sa[Rank[i]-]; r[i+k]==r[j+k]; k++);
}
}
int dp[MAXN][];
void RMQ(){
for(int i = ; i <= n; i++){
dp[i][] = height[i];
}
for(int i = ; i < ; i++){
for(int j = ; j + ( << i) - <= n; j++){
dp[j][i] = min(dp[j][i-],dp[(j+(<<(i-)))][i-]);
}
}
}
int getnum(int x,int y){
x = Rank[x];
y = Rank[y];
if(x > y)swap(x,y);
x += ;
int k = (int)((log(y - x + ) * 1.0) / log(2.0));
return min(dp[x][k],dp[y - (<<k) + ][k]);
}
void solve(){
int ans = ;
for(int i = ; i < n; i++){
for(int j = ; j + i < n; j += i){
int ret = getnum(j,j+i);
int t = ret / i + ;// behind r[i] can repeat t times
int tp = j - (i - ret % i);
if(tp >= && (ret % i != ) && getnum(tp,tp+i) >= ret){
t ++;
}
ans = max(t,ans);
}
}
printf("%d\n",ans);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
getchar();
for(int i = ; i < n; i++){
scanf("%c",&s[i]);
r[i] = s[i];
getchar();
}
r[n] = ;
da(r,sa,n+,);
calheight(r,sa,n);
RMQ();
solve();
}
return ;
}

spoj687 后缀数组重复次数最多的连续重复子串的更多相关文章

  1. POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)

    题意: 给出一个串,求重复次数最多的连续重复子串 分析: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现几次. 既然长度为L的串重复出现,那么str[0],str[l],str ...

  2. poj 3693 后缀数组 重复次数最多的连续重复子串

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8669   Acc ...

  3. POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串

    题目链接:https://vjudge.net/problem/POJ-3693 Maximum repetition substring Time Limit: 1000MS   Memory Li ...

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

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

  5. 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串

    后缀数组的论文里的例题,论文里的题解并没有看懂,,, 求一个重复次数最多的连续重复子串,又因为要找最靠前的,所以扫的时候记录最大的重复次数为$ans$,扫完后再后从头暴力扫到尾找重复次数为$ans$的 ...

  6. POJ - 3693 Maximum repetition substring(重复次数最多的连续重复子串)

    传送门:POJ - 3693   题意:给你一个字符串,求重复次数最多的连续重复子串,如果有一样的,取字典序小的字符串. 题解: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现 ...

  7. Repeats SPOJ - REPEATS(重复次数最多的连续重复子串)

    论文题例8 https://blog.csdn.net/queuelovestack/article/details/53031731这个解释很好 其实,当枚举的重复子串长度为i时,我们在枚举r[i* ...

  8. Maximum repetition substring POJ - 3693(重复次数最多的连续重复子串)

    这题和SPOJ - REPEATS 一样  代码改一下就好了 这个题是求这个重复子串,还得保证字典序最小 巧妙运用sa 看这个 https://blog.csdn.net/queuelovestack ...

  9. 687. Repeats spoj (后缀数组 重复次数最多的连续重复子串)

    687. Repeats Problem code: REPEATS A string s is called an (k,l)-repeat if s is obtained by concaten ...

随机推荐

  1. notepad++编译并运行java (自定义包)

    最近用Notepad++写汇编,感觉用起来挺顺手,于是想能不能也在这个优秀的编辑器下编写java并编译运行呢,因为每次启动eclipse都要挺长时间,而且eclipse实在太占内存了... 于是各种百 ...

  2. 《MySchool数据库设计优化》内部测试

    1) 在SQL Server 中,为数据库表建立索引能够( C ). A. 防止非法的删除操作 B. 防止非法的插入操作 C. 提高查询性能 D. 节约数据库的磁盘空间 解析:索引的作用是通过使用索引 ...

  3. [No000057]一个人默默背单词,小心被传染哦

    不日凛冬将至,全国各地,已有多名少侠因季节变化,出现了不同程度的四肢不勤.bd不分的症状.具体表现为—— 包大人在此高能预警:不想背单词,有可能你已经被传染了. 好好的,怎么突然不想背单词了 哈佛医学 ...

  4. eclipse 编译android程序 编译错误

    windows->show view -> problems, 这个窗口的内容即为 编译错误的内容.

  5. wireshake抓包,飞秋发送信息,python

    http://wenku.baidu.com/link?url=Xze_JY8T15pqI9mBLRpTxWF2d6MP-32xb6UwuE6tsUmitRDheJe-Ju87WlDEDBGuI5MF ...

  6. sqlserver 通用分页存储过程

    来源:http://www.jb51.net/article/19936.htm CREATE PROCEDURE commonPagination ), --要显示的列名,用逗号隔开 ), --要查 ...

  7. mac系统上使用压缩包版的mysql(非安装版)

    mac本换了块固态硬盘,一切重新装过,mysql嫌官网下载太慢,直接百度 "mysql mac",第一个就是: 不料下载完后,发现这是一个压缩包版,并没有安装程序.网上搜索了一下, ...

  8. TF400916错误修复办法

    在使用TFS作为研发过程管理工具的时候,如果调整了工作项的状态信息,可能会出现下面的错误: 要解决此问题非常简单: 1.找一台安装了VS2015程序的环境.因为我们使用的是TFS2015,所以需要对应 ...

  9. Cocos2d-x 3.4在AndroidStudio上编译配置

    转载请标明出处:http://www.cnblogs.com/studweijun/p/4320778.html 1.准备好以下文件 1) AndroidStudio:  https://dl.goo ...

  10. 简单高效的nodejs爬虫模型

    这篇文章讲解一下yunshare项目的爬虫模型. 使用nodejs开发爬虫很简单,不需要类似python的scrapy这样的爬虫框架,只需要用request或者superagent这样的http库就能 ...