题目链接:https://cn.vjudge.net/contest/283743#problem/D

题目大意:给你n个数,然后问你出现m次的最长子串的长度。

具体思路:和上一篇博客的内容差不多,这个是可重复的,就不需要考虑sa的问题了,每一次还是二分答案,判断出现的最长前缀就可以了。注意二分的时候,每一次的寻找,初始值为1,因为这个字符串就已经出现过一次了。

AC代码:

 #include<iostream>
#include<stack>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 5e6+;
int cntA[maxn], cntB[maxn], sa[maxn], tsa[maxn], A[maxn], B[maxn], height[maxn];
int Rank[maxn];
int ch[maxn];
int sto[maxn];
ll n,m;
//sa[i]代表第i小的后缀位置,Rank[i]代表第i位置的后缀,排名第几小
// height[i]代表排名第i个字符串和第i-1个字符串的相同前缀有多少个
void cal(int maxx)
{
for(int i = ; i <=maxx; i++)
cntA[i] = ;
// cout<<1<<endl;
// cout<<n<<endl;
for(int i = ; i <= n; i++)
{
//cout<<ch[i-1]<<endl;
cntA[ch[i-]]++;
}
// cout<<1<<endl;
for(int i = ; i <= maxx; i++)
cntA[i] += cntA[i-];
for(int i = n; i; i--)
sa[cntA[ch[i-]]--] = i;
Rank[sa[]] = ;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(ch[sa[i]-] != ch[sa[i-]-])
Rank[sa[i]]++;
}
for(int l = ; Rank[sa[n]] < n; l <<= )
{
memset(cntA, , sizeof(cntA));
memset(cntB, , sizeof(cntB));
for(int i = ; i <= n; i++)
{
cntA[A[i] = Rank[i]]++;
cntB[B[i] = (i+l <= n)?Rank[i+l]:]++;
}
for(int i = ; i <= n; i++)
cntB[i] += cntB[i-];
for(int i = n; i; i--)
tsa[cntB[B[i]]--] = i;
for(int i = ; i <= n; i++)
cntA[i] += cntA[i-];
for(int i = n; i; i--)
sa[cntA[A[tsa[i]]]--] = tsa[i];
Rank[sa[]]=;
for(int i = ; i <= n; i++)
{
Rank[sa[i]] = Rank[sa[i-]];
if(A[sa[i]] != A[sa[i-]] || B[sa[i]] != B[sa[i-]])
Rank[sa[i]]++;
}
}
for(int i = , j = ; i <= n; i++)
{
if(j)
j--;
while(ch[i+j-] == ch[sa[Rank[i]-] + j - ])
j++;
height[Rank[i]] = j;
}
}
bool judge(int t)
{
int ans=;
for(int i=; i<=n; i++)
{
if(height[i]>=t)
{
ans++;
}
else
{
ans=;
}
if(ans>=m)
return true;
}
return false;
}
int main()
{
int maxx=;
scanf("%lld %lld",&n,&m);
for(int i=; i<=n; i++)
{
scanf("%d",&ch[i]);
maxx=max(maxx,ch[i]);
}
cal(maxx);
int l=,r=1e8,ans=;
while(l<=r)
{
int mid=(l+r)>>;
if(judge(mid))
{
ans=mid;
l=mid+;
}
else
{
r=mid-;
}
}
printf("%d\n",ans);
return ;
}

D - Milk Patterns (出现k次可重复的最长子串的长度)的更多相关文章

  1. LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

    题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...

  2. POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)

    题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...

  3. 【LeetCode】无重复字符串最长子串

    题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...

  4. 【leetcode-03】给定一个字符串,请你找出其中不含有重复字符的最长子串的长度

    开个新坑,leetcode上面做题目.下面是题目描述: <!-- 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出 ...

  5. POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

    Milk Patterns   Description Farmer John has noticed that the quality of milk given by his cows varie ...

  6. POJ 3261 出现至少K次的可重叠最长子串

    题意就是给一列数字,求最长的一个子串,并且满足子串在原数串中出现至少K次,子串可以重叠. 解法是将问题转为判定性问题,二分子串的长度,判定是否满足重复至少K次.判定方法是经典的根据子串长度将Heigh ...

  7. [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  8. UVa 11107 生命的形式(不小于k个字符串中的最长子串)

    https://vjudge.net/problem/UVA-11107 题意:给定n个字符串,求出现在不小于n的一半个字符串的最长子串,如果有多个,则按字典序输出. 思路: 首先就是将这n个字符串连 ...

  9. poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16223   Accepted: 4763 Descr ...

随机推荐

  1. hashCode和equal

    转自:https://www.cnblogs.com/dolphin0520/p/3681042.html hashCode方法在Object类中: public native int hashCod ...

  2. luogu4197 Peaks (kruskal重构树+主席树)

    按照边权排序建出kruskal重构树,每次就变成了先找一个权值<=x的最远的祖先,然后看这个子树的第k小.离散化一下,在dfs序上做主席树即可 而且只需要建叶节点的主席树 注意输出的是第k小点的 ...

  3. 【bzoj3938】 Robot

    http://www.lydsy.com/JudgeOnline/problem.php?id=3938 (题目链接) 题意 给出数轴上$n$个点,有$m$个操作,在时间$t$让一个点以一定的速度移动 ...

  4. Java -- JDBC_DAO 设计模式

    DAO:Date Access Object 实现代码模块化,更加有利于代码的维护和升级. DAO 可以被子类继承或者直接使用. 访问数据信息的类,包含对数据的CRUD(create read upd ...

  5. ECMAScript 6 -- let和const命令

    ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. for (let i = 0; i ; i++) {console.log(i);} ...

  6. 两场CF

    分别是正规赛998和虚拟赛935 998我神速A了前三题之后挂了,第四题是一个打表找规律题然而我并没有想到打表... 然后靠着速度拿到470名,上了蓝名.这告诉我们:输入数据是一个数/两个数(noip ...

  7. 网络流 KM dinic

    study from: https://blog.csdn.net/A_Comme_Amour/article/details/79356220 1. Edmonds-Karp 无优化 最坏时间复杂度 ...

  8. genetic model

    如果CC表示野生基型,CA因表示杂合型突变基因型,AA表示纯合型突变基因型.Recessive Model(隐性模型 ):AA VS (CA+CC);Dominant Model(显性模型):(CA+ ...

  9. MyEclipse中引用的maven配置文件只访问私服的配置

    MyEclipse中要用到集成的maven,公司内网有个私服,办公机不能上外网. 这时Eclipse中设置引用的外部Setting配置文件中只需如下配置即可: 1.配置本地主机的maven仓库路径 & ...

  10. Kafka+Log4j2日志

    默认你已经安装配置了Zookeeper和Kafka. 为了目录清晰,我的Kafka配置文件的Zookeeper部分是:加上了节点用来存放Kafka信息 启动Zookeeper,然后启动Kafka. Z ...