2018.07.17 牛奶模式Milk Patterns(二分+hash)
传送门
一道简单的字符串。这里收集了几种经典做法:
- SAM,不想写。
- 后缀数组+二分,不想写
- 后缀数组+单调队列,不想写
- hash+二分,for循哈希,天下无敌!于是妥妥的hash
代码如下:
#include<bits/stdc++.h>
#define N 20005
#define Base 20001
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
return ans;
}
inline void write(int x){
if(x>9)write(x/10);
putchar(x%10+'0');
}
int n,a[N],b[N],k;
map<unsigned int,int>mp;
unsigned int sum[N],bas[N];
int main(){
n=read(),k=read();
bas[0]=1;
for(int i=1;i<=n;++i)a[i]=b[i]=read(),bas[i]=bas[i-1]*Base;
sort(b+1,b+n+1);
int siz=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;++i){
a[i]=lower_bound(b+1,b+siz+1,a[i])-b;
sum[i]=sum[i-1]*Base+a[i];
}
int l=0,r=n;
while(l<=r){
mp.clear();
int mid=l+r>>1,cnt=0;
for(int i=1;i+mid<=n;++i)cnt=max(cnt,++mp[sum[i+mid]-sum[i-1]*bas[mid+1]]);
if(cnt>=k)l=mid+1;
else r=mid-1;
}
printf("%d",l);
return 0;
}
2018.07.17 牛奶模式Milk Patterns(二分+hash)的更多相关文章
- [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns
洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...
- luogu P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组 + Height数组 + 二分答案 + 扫描
后缀数组有一个十分有趣的性质: $height[rk[i]] >= height[rk[i-1]] - 1$ Code: #include <bits/stdc++.h> #d ...
- 【后缀数组】【LuoguP2852】 [USACO06DEC]牛奶模式Milk Patterns
题目链接 题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". J ...
- [Luogu2852][USACO06DEC]牛奶模式Milk Patterns
Luogu 一句话题意 给出一个串,求至少出现了\(K\)次的子串的最长长度. sol 对这个串求后缀数组. 二分最长长度. 如果有\(K\)个不同后缀他们两两的\(lcp\)都\(>=mid\ ...
- 洛谷P2852 牛奶模式Milk Patterns [USACO06DEC] 字符串
正解:SA/二分+哈希 解题报告: 传送门! umm像这种子串的问题已经算是比较套路的了,,,?就后缀的公共前缀这样儿的嘛QwQ 所以可以先求个SA 然后现在考虑怎么判断一个长度为d的子串出现了k次? ...
- [USACO06DEC] 牛奶模式Milk Patterns
题目链接:戳我 我们知道后缀数组的h数组记录的是后缀i和后缀i-1的最长公共前缀长度,后缀的前缀其实就是子串. 因为是可以重复出现的子串,所以我们只要计算哪些h数组的长度大于等于x即可.这一步操作我们 ...
- P2852 [USACO06DEC]牛奶模式Milk Patterns
link 这是一道后缀匹配的模板题 我们只需要将height算出来 然后二分一下答案就可以了 #include<cstdio> #include<algorithm> #inc ...
- Luogu P2852 [USACO06DEC]牛奶模式Milk Patterns
题目链接 \(Click\) \(Here\) 水题.利用\(Height\)的性质维护一个单调栈即可. #include <bits/stdc++.h> using namespace ...
- 2018.07.03 POJ 2318 TOYS(二分+简单计算几何)
TOYS Time Limit: 2000MS Memory Limit: 65536K Description Calculate the number of toys that land in e ...
随机推荐
- Posting JSON to Spring MVC Controller
Spring MVC can be setup to automatically bind incoming JSON string into a Java object. Firstly, ensu ...
- FreeMarkerUtl
/** * @title FreeMarkerUtl * @description 模板文件工具类 * @author maohuidong * @date 2017-07-05 */public c ...
- Nginx安装部署以及配置文件解析
Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令.Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的”/uri/”,可以是字符串或 ...
- homework 补第三题
上次只写了两道题 发现少了一道 所以今天补上 4 . 另外补上今天的程序代码 代码 1. 2. 3 4 运行截图 1. 2 3
- CentOS6.5下安装ActiveMQ
1.下载ActiveMQ [root@localhost softwares]# wget http://archive.apache.org/dist/activemq/apache-activem ...
- sqlserver批量导出存储过程、函数、视图
select text from syscomments s1 join sysobjects s2 on s1.id=s2.id where xtype = 'V' xtype V 视图 P ...
- hdoj1010 奇偶剪枝+DFS
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- np.random.random()系列函数
1.np.random.random()函数参数 np.random.random((1000, 20)) 上面这个就代表生成1000行 20列的浮点数,浮点数都是从0-1中随机. 2.numpy.r ...
- python 的时间与日期
显示当前日期: import time print time.strftime('%Y-%m-%d %A %X %Z',time.localtime(time.time())) 或者 你也可以用: p ...
- python之字符串【str】
#Auther Bob#--*--conding:utf-8 --*-- #定义一个str的对象,有下面两种方法name = 'Bob abc'job = str('it')print(type(na ...