[HDU 4821] String (字符串哈希)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821
题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个?
哈希+枚举
我就是不会枚举这样的,这次涨姿势了。
每次枚举起点,然后就能枚举全部的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long ull; const ull B = **+;
int M,L;
char s[];
ull h[];
ull base[];
ull tt[]; int main(){
base[] = ;
for(int i=;i<;i++) base[i] = base[i-]*B;
while(scanf("%d%d",&M,&L)!=EOF){
memset(h,,sizeof(h));
scanf("%s",s);
int len = strlen(s);
for(int i=;i<len;i++){
if( i== ) h[i] = s[i];
else h[i] = h[i-]*B + s[i];
}
int ans = ;
// printf("len = %d\n",len);
for(int i=;i<L;i++){ // 枚举全部起点
// puts("*******************");
int cnt = ;
map<ull,int> H;
for(int j=i;j+L<=len;j+=L){ // 枚举每段
tt[cnt++] = h[j+L-] - (j==?:(h[j-]*base[L]));
// printf("%d => %llu\n",j,tt[cnt-1]);
}
// printf("cnt=%d\n",cnt);
// 迟取法取每段然后数数,注意这里的j<min(cnt,M)
for(int j=;j<min(cnt,M);j++){
// printf("******%llu\n",H[tt[j]]);
H[tt[j]]++;
}
if( H.size()==M ){
ans++;
// printf("%d==%d\n",H.size(),M);
}
for(int j=M;j<cnt;j++){
H[tt[j-M]]--;
if( H[tt[j-M]]== ) H.erase(tt[j-M]);
H[tt[j]]++;
if( H.size()==M ){
ans++;
// printf("%d==%d\n",H.size(),M);
}
}
}
printf("%d\n",ans);
}
return ;
}
[HDU 4821] String (字符串哈希)的更多相关文章
- HDU 4821 String 字符串hash
String Problem Description Given a string S and two integers L and M, we consider a substring of S ...
- HDU 3973 AC's String 字符串哈希
HDU 3973 通过哈希函数将一个字符串转化为一个整数,通过特定的方式可以使得这个哈希值几乎没有冲突(这就是关键之处,几乎没有视为没有= =!, 其实也可以考虑实现哈希冲突时的处理,只是这道题没必要 ...
- HDU 4821 String(BKDRHash)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给出一个字符串,现在问你可以找出多少个长度为M*L的子串,该子串被分成L个段,并且每个段的字符串都是 ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
- HDU 4821 String (HASH)
题意:给你一串字符串s,再给你两个数字m l,问你s中可以分出多少个长度为m*l的子串,并且子串分成m个长度为l的串每个都不完全相同 首先使用BKDRHash方法把每个长度为l的子串预处理成一个数字, ...
- HDU 4821 String hash
String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU - 4821 String(窗口移动+map去重+hash优化)
String Given a string S and two integers L and M, we consider a substring of S as “recoverable” if a ...
- TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Hdu 4821 (字符串hash+map)
题目链接https://vjudge.net/problem/HDU-4821 题意:给定字符串S ,询问用几个子串满足 : 1.长度为n*len . 2. n个子串都不相同. 题解:倒序hash将 ...
随机推荐
- python子类分配
原问题是将左边样式变成右边样式: 即有父类和子类,父类包括多个子类,怎样将子类匹配到父类下面的问题 代码如下 #!/usr/bin/python3.4 # -*- coding: utf-8 -*- ...
- Java模板引擎 HTTL
新一代java模板引擎典范 Beetl http://www.oschina.net/p/httl HTTL(Hyper-Text Template Language)是一个高性能的开源JAVA模板引 ...
- Js数组去重复取唯一值
function isBigEnough(element) { return element >= 10; } var filtered = [12, 5, 8, 130, 44].filter ...
- Zabbix 教程
Zabbix 教程http://blog.csdn.net/linuxlsq/article/details/52606086 MySQL在以下几种情况会创建临时表:1.UNION查询:2.用到TEM ...
- Httpservlet cannot be resolved to a type的原因与解决方法
刚开始学习Servlet,在Eclipse中新建了一个Servlet,不过页面上报错: Httpservlet cannot be resolved to a type,显然是Eclipse找不到相应 ...
- Hive(一):架构及知识体系
Hive是一个基于Hadoop的数据仓库,最初由Facebook提供,使用HQL作为查询接口.HDFS作为存储底层.mapReduce作为执行层,设计目的是让SQL技能良好,但Java技能较弱的分析师 ...
- mysql查询表中的所有字段
select column_name, table_name from information_schema.columns where table_schema='yizhan' and colum ...
- shopex用户登陆错误提示在nginx下乱码问题
http://www.test.cn/passport-aHR0cDosLHd3dy54eTAwNy5jbixwYXNzcG9ydC1hSFIwY0Rvc0xIZDNkeTU0ZVRBd055NWpi ...
- sqlserver 登录失败——孤立用户
USE (数据库实例)hhwz; GO sp_change_users_login @Action='update_one', @UserNamePattern='数据库用户', @LoginName ...
- Android反向工程需要的几个软件
1.apktoolapktool d xxx.apk 得到全部的资源素材 2.dex2jardex2jar classes.dex 3.jd-gui把jar文件转成 .java的源代码