LG2852/BZOJ1717 「USACO2006DEC」Milk Patterns 离散化+后缀数组
问题描述
题解
字符串性质:字符串\(s\)的每个字串等于每个后缀的所有前缀
对输入的东西离散化,然后把数值看做\(\mathrm{ASCII}\)后缀排序
二分答案,二分长度。
显然一段相同的字串,一定是连续一段后缀的公共前缀。
如此\(check\)即可。
\(\mathrm{Code}\)
#include<bits/stdc++.h>
using namespace std;
#define maxn 20007
void read(int &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') fh=-1,ch=getchar();
else fh=1;
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
x*=fh;
}
struct node{
int val,id,New;
}st[maxn];
int sa[maxn],n,m,ct[maxn],x[maxn],y[maxn],tot;
int hei[maxn];
int a[maxn],cnt,rk[maxn];
int low,l,r,mid,ans;
bool comp(node a,node b){
return a.val<b.val;
}
bool cmp(node a,node b){
return a.id<b.id;
}
void preprocess(){
sort(st+1,st+n+1,comp);
for(register int i=1;i<=n;i++){
if(i==1||(i>1&&st[i].val>st[i-1].val)){
m++;
}
st[i].New=m;
}
sort(st+1,st+n+1,cmp);
for(register int i=1;i<=n;i++){
a[i]=st[i].New;
}
++m;
}
void SA(){
for(register int i=1;i<=n;i++) ct[x[i]=a[i]]++;
for(register int i=2;i<=m;i++) ct[i]+=ct[i-1];
for(register int i=n;i>=1;i--) sa[ct[x[i]]--]=i;
for(register int k=1;k<=n;k<<=1){
int tot=0;
for(register int i=n-k+1;i<=n;i++) y[++tot]=i;
for(register int i=1;i<=n;i++) if(sa[i]>k) y[++tot]=sa[i]-k;
for(register int i=1;i<=m;i++) ct[i]=0;
for(register int i=1;i<=n;i++) ct[x[i]]++;
for(register int i=1;i<=m;i++) ct[i]+=ct[i-1];
for(register int i=n;i>=1;i--) sa[ct[x[y[i]]]--]=y[i],y[i]=0;
swap(x,y);x[sa[1]]=tot=1;
for(register int i=2;i<=n;i++)
if(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]) x[sa[i]]=tot;
else x[sa[i]]=++tot;
if(tot==n) break;
m=tot;
}
}
void HEIGHT(){
int tmp=0;
for(register int i=1;i<=n;i++) rk[sa[i]]=i;
for(register int i=1;i<=n;i++){
if(rk[i]==1) continue;
if(tmp) --tmp;
int j=sa[rk[i]-1];
while(j+tmp<=n&&i+tmp<=n&&a[i+tmp]==a[j+tmp]) ++tmp;
hei[rk[i]]=tmp;
}
}
bool check(){
if(mid==0) return true;
int lst=0;
for(register int i=1;i<=n;i++){
if(hei[i]<mid){//错误笔记:将mid写为m,以后check写传参式的
if(i-lst>=low) return true;
lst=i;
}
}
if(n+1-lst>=low) return true;
return false;
}
int main(){
read(n);read(low);
for(register int i=1;i<=n;i++){
read(st[i].val);st[i].id=i;
}
preprocess();
SA();HEIGHT();
l=0,r=n;
while(l<=r){
mid=(l+r)>>1;
if(check()) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
return 0;
}
LG2852/BZOJ1717 「USACO2006DEC」Milk Patterns 离散化+后缀数组的更多相关文章
- POJ 3261 Milk Patterns(后缀数组+二分答案+离散化)
题意:给定一个字符串,求至少出现k 次的最长重复子串,这k 个子串可以重叠. 分析:经典的后缀数组求解题:先二分答案,然后将后缀分成若干组.这里要判断的是有没有一个组的符合要求的后缀个数(height ...
- poj 3261 Milk Patterns(后缀数组)(k次的最长重复子串)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7938 Accepted: 3598 Cas ...
- POJ 3261 Milk Patterns 【后缀数组 最长可重叠子串】
题目题目:http://poj.org/problem?id=3261 Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Subm ...
- POJ 3261 Milk Patterns (后缀数组,求可重叠的k次最长重复子串)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16742 Accepted: 7390 Ca ...
- liberOJ #2033. 「SDOI2016」生成魔咒 后缀数组
#2033. 「SDOI2016」生成魔咒 题目描述 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1 11.2 22 拼凑起来形成一个魔咒串 [1,2] [1, 2] ...
- 【BZOJ1717&POJ3261】Milk Patterns(后缀数组,二分)
题意:求字符串的可重叠的k次最长重复子串 n<=20000 a[i]<=1000000 思路:后缀数组+二分答案x,根据height分组,每组之间的height>=x 因为可以重叠, ...
- poj3261 Milk Patterns【后缀数组】【二分】
Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...
- POJ-3261 Milk Patterns(后缀数组)
题目大意:找出至少出现K次的子串的最长长度. 题目分析:二分枚举长度x,判断有没有最长公共前缀不小于x的并且连续出现了至少k次的有序子串区间. 代码如下: # include<iostream& ...
- poj3261 Milk Patterns(后缀数组)
[题目链接] http://poj.org/problem?id=3261 [题意] 至少出现k次的可重叠最长子串. [思路] 二分长度+划分height,然后判断是否存在一组的数目不小于k即可. 需 ...
随机推荐
- 洛谷P4169 [Violet]天使玩偶/SJY摆棋子
%%%神仙\(SJY\) 题目大意: 一个二维平面,有两种操作: \(1.\)增加一个点\((x,y)\) \(2.\)询问距离\((x,y)\)曼哈顿最近的一个点有多远 \(n,m\le 300 0 ...
- Exception "java.lang.ClassNotFoundException: com/intellij/codeInsight/editorActions/FoldingData"while constructing DataFlavor for: application/x-java-jvm-local-objectref; class=com.intellij.codeInsigh
java.lang.ClassNotFoundException in matlabR2014a 就是MATLAB和pycharm不能同时运行.关闭pycharm然后打开MATLAB就可以了.
- AGC037C Numbers on a Circle(神奇思路)
Atcoder 全是神仙题-- 先变成能不能从 \(b\) 到 \(a\).操作变成一个数减掉旁边两个数. 考虑里面最大的且不和 \(a\) 中相等的那个数.它两边的数此时都不能操作,否则就减到非正数 ...
- python-8-字符串索引与切片
前言 python访问字符串的值,可以使用方括号来截取字符串,但切片对原来的值是不会改变,如下: 一.索引 1.索引下标查找 # 1.索引 a = 'ABCDPOM' s = a[0] s2 = a[ ...
- 联邦学习PySyft
Steps involved in the Federated Learning Approach The mobile devices download the global ML model Da ...
- Srinath总结 架构师们遵循的 30 条设计原则
作者:Srinath 翻译:贺卓凡,来源:公众号 ImportSource Srinath 通过不懈的努力最终总结出了 30 条架构原则,他主张架构师的角色应该由开发团队本身去扮演,而不是专门有个架构 ...
- 【UOJ#48】【UR #3】核聚变反应强度(质因数分解)
[UOJ#48][UR #3]核聚变反应强度(质因数分解) 题面 UOJ 题解 答案一定是\(gcd\)除掉\(gcd\)的最小质因子. 而\(gcd\)的最小值因子一定是\(a_1\)的质因子. 所 ...
- Logstash:把MySQL数据导入到Elasticsearch中
Logstash:把MySQL数据导入到Elasticsearch中 前提条件 需要安装好Elasticsearch及Kibana. MySQL安装 根据不同的操作系统我们分别对MySQL进行安装.我 ...
- linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));
static void Main(string[] args) { int[] arr = { 1, 3, 3, 3, 3, 4, 5, 4, 5, 8, 9, 3 }; //不重复 var uniq ...
- windows下安装pip教程
下载地址是:https://pypi.python.org/pypi/pip#downloads 下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入: python setup.py i ...