题意:

给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少

思路:

我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[0]、s[L]、s[2L] \cdots s[L * k]\)中至少有两个连续的位置是相同的,然后看字母\(s[L * i]和s[L * (i + 1)]\)往前往后最多能匹配多远,记住总长度\(len\),那么最大循环次数为\((len / L) + 1\)。

参考:

SPOJ 687. Repeats(后缀数组)

代码:

#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 50000 + 5;
const int INF = 0x3f3f3f3f;
const ull seed = 11;
const int MOD = 1e9 + 7;
using namespace std; //下标从0开始
int str[maxn]; //str[n]赋值一个最小值0,其他大于0
int t1[maxn], t2[maxn], c[maxn];
int sa[maxn]; //排名为i的后缀下标
int rk[maxn]; //后缀下标为i的排名
int height[maxn]; //sa[i]与sa[i - 1]的LCP
int mm[maxn];
int dp[maxn][30];
bool cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *str, int n, int m){
n++;
int i, j, p, *x = t1, *y = t2;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = str[i]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
for(j = 1; j <= n; j <<= 1){
p = 0;
for(i = n - j; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j)? p - 1 : p++;
if(p >= n) break;
m = p;
}
int k = 0;
n--;
for(i = 0; i <= n; i++) rk[sa[i]] = i;
for(i = 0; i < n; i++){
if(k) k--;
j = sa[rk[i] - 1];
while(str[i + k] == str[j + k]) k++;
height[rk[i]] = k;
}
}
void initRMQ(int n){
mm[0] = -1;
for(int i = 1; i <= n; i++){
dp[i][0] = height[i];
mm[i] = ((i & (i - 1)) == 0)? mm[i - 1] + 1 : mm[i - 1];
}
for(int j = 1; j <= mm[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 RMQ(int L, int R){
int k = mm[R - L + 1];
return min(dp[L][k], dp[R - (1 << k) + 1][k]);
}
int LCP(int i, int j){ //求后缀i和j的LCP最长公共前缀
int L = rk[i], R = rk[j];
if(L > R) swap(L, R);
L++;
return RMQ(L, R);
} int main(){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++){
char ss[2];
scanf("%s", ss);
str[i] = ss[0] - 'a' + 1;
}
str[n] = 0;
da(str, n, 3);
initRMQ(n);
int ans = 1;
for(int i = 1; i < n; i++){
for(int j = 0; j + i < n; j += i){
int len = LCP(j, j + i);
int times = len / i + 1;
int pos = j - (i - len % i);
if(pos >= 0){
len = LCP(pos, pos + i);
times = max(times, len / i + 1);
}
ans = max(ans, times);
}
}
printf("%d\n", ans);
}
return 0;
}

SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解的更多相关文章

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

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

  3. POJ 3693 后缀数组+RMQ

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

  4. 【SPOJ – REPEATS】 后缀数组【连续重复子串】

    字体颜色如何 字体颜色 SPOJ - REPEATS 题意 给出一个字符串,求重复次数最多的连续重复子串. 题解 引自论文-后缀数组--处理字符串的有力工具. 解释参考博客 "S肯定包括了字 ...

  5. SPOJ 687 Repeats(后缀数组+ST表)

    [题目链接] http://www.spoj.com/problems/REPEATS/en/ [题目大意] 求重复次数最多的连续重复子串的长度. [题解] 考虑错位匹配,设重复部分长度为l,记s[i ...

  6. SPOJ Repeats(后缀数组+RMQ-ST)

    REPEATS - Repeats no tags  A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...

  7. Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)

    Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...

  8. Ural1297 最长回文子串(后缀数组+RMQ)

    /* 源程序丢失QWQ. 就不粘代码了. 大体做法是把串反转然后连接. 做一遍后缀数组. 对height做一遍rmq. 然后对于每个位置的奇偶分别判断, 记下pos. 注意求的是[l+1,r]的hei ...

  9. SPOJ 694 || 705 Distinct Substrings ( 后缀数组 && 不同子串的个数 )

    题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀 ...

随机推荐

  1. 图解 ECDHE 密钥交换算法

    HTTPS 常用的密钥交换算法有两种,分别是 RSA 和 ECDHE 算法. 其中,RSA 是比较传统的密钥交换算法,它不具备前向安全的性质,因此现在很少服务器使用的.而 ECDHE 算法具有前向安全 ...

  2. 如何在 crontab 中让 source ~/.bashrc 生效

    cron 是许多类 Unix 操作系统中都自带的用来调度定时任务的工具,定时任务的配置是写在 crontab 文件中的,但是 crontab 文件不允许直接编辑,一般都是通过命令 crontab -e ...

  3. 30分钟带你了解「消息中间件」Kafka、RocketMQ

    消息中间件的应用场景 主流 MQ 框架及对比 说明 Kafka 优点 Kafka 缺点 RocketMQ Pulsar 发展趋势 各公司发展 Kafka Kafka 是什么? Kafka 术语 Kaf ...

  4. Markdown里常用的HTML元素

    转义:\ 换行:<br/> 红色文字:<font color=#FF0000>字体改成红色了</font> A标签 新窗口:<a href="xxx ...

  5. XShell的手动直连,避免配置ssh免密的一些问题

  6. Cannot assign requested address问题总结

    Cannot assign requested address问题总结 - 简书 https://www.jianshu.com/p/51a953b789a4 python3 server.pyE07 ...

  7. Python学习【第2篇】:循环

    For循环 pass while 循环 pass 练习题: 1.使用while循环输入 1 2 3 4 5 6     8 9 10,不输出7 n = 1while n< 11: if n == ...

  8. cachedThreadPool缓存线程池

    package com.loan.modules.common.util; import java.util.concurrent.BlockingQueue; import java.util.co ...

  9. python--基础2 (数据类型及应用)

    资源池 链接:https://pan.baidu.com/s/1OGq0GaVcAuYEk4F71v0RWw 提取码:h2sd python数据类型 字符串 列表 字典 数字(整数) 数字(浮点数) ...

  10. jqXHR.fail()回调方法及其参数详细说明

    jqXHR.fail()是一个可供选择的 error 回调选项的构造函数,.fail()方法取代了的过时的.error()方法.从 jQuery 1.5 开始,$.ajax()返回的jqXHR对象 实 ...