HDU 4821 String (HASH)
题意:给你一串字符串s,再给你两个数字m l,问你s中可以分出多少个长度为m*l的子串,并且子串分成m个长度为l的串每个都不完全相同
首先使用BKDRHash方法把每个长度为l的子串预处理成一个数字,接着根据题意直接map判重
BKDRHash:一种常用字符串hash,hash简单来说就是把一串字符串通过一些转化成为一个数字,并保证相同字符串转化的数字一样,不相同字符串转化的数字一定不一样。方法就是hash[i]=hash[i-1]*seed(进制)+str[i]-'a'+1(注意要加一,因为不能为0)
注意这儿unsigned long long可以自动取模,还有BKDRHash需要取进制31,131这些。
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
const ull seed=;//31 131 1313 ......
char str[Max];
ull hhash[Max],base[Max];//ull自动取模
map<ull,int> mp;//判重
void Init()//初始化seed倍数,用于hash删前一个字符
{
base[]=1ull;
for(int i=; i<Max; ++i)
base[i]=base[i-]*seed;
return;
}
void Hash(int l,int len)//把每l个字符压缩成为一个数字
{
for(int i=; i<len; ++i)
{
if(i>=l)//首先删除前一个字符
hhash[i]=hhash[i-]-base[l-]*(str[i-l]-'a'+);
else if(i)
hhash[i]=hhash[i-];
else
hhash[]=0ull;
hhash[i]=hhash[i]*seed+str[i]-'a'+;//hash
//printf("%llu\n",hhash[i]);
}
return ;
}
int Solve(int m,int l,int len)
{
int ans=,i;
int now,sum;//记录当前运行到第几个 记录总共有多少种值
Hash(l,len);//存下每l位map判重
for(int i=l-;i<l+l-;++i)
{
now=sum=;
mp.clear();
for(int j=i;j<len;j+=l)
{
now++;
if(now>m)//删除前面超出区间的值
{
mp[hhash[j-m*l]]--;
if(mp[hhash[j-m*l]]==)
sum--;
}
if(!mp.count(hhash[j])||mp[hhash[j]]==)//此值在此子区间没有
{
mp[hhash[j]]=;
sum++;
}
else
mp[hhash[j]]++;
if(sum==m)
ans++;
}
}
return ans;
}
int main()
{
int m,l;
Init();//初始化
while(~scanf("%d %d",&m,&l))
{
scanf("%s",str);
printf("%d\n",Solve(m,l,strlen(str)));
}
return ;
}
HDU 4821 String (HASH)的更多相关文章
- HDU 4821 String(BKDRHash)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给出一个字符串,现在问你可以找出多少个长度为M*L的子串,该子串被分成L个段,并且每个段的字符串都是 ...
- 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 ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
- HDU 4821 String 字符串hash
String Problem Description Given a string S and two integers L and M, we consider a substring of S ...
- HDU 2523 sort (hash)
#include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...
- HDU - 1496 Equations (hash)
题意: 多组测试数据. 每组数据有一个方程 a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0,方程中四个未知数 x1, x2, x3, x4 ∈ [-100, 100], 且 ...
- Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)
help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...
- 哈希(Hash)与加密(Encrypt)相关内容
1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...
- 【Redis】命令学习笔记——哈希(hash)(15个超全字典版)
本篇基于redis 4.0.11版本,学习哈希(hash)相关命令. hash 是一个string类型的field和value的映射表,特别适合用于存储对象. 序号 命令 描述 实例 返回 HSET ...
随机推荐
- java web 学习 --第九天(Java三级考试)
第八天的学习内容如下:http://www.cnblogs.com/tobecrazy/p/3468458.html Java servlet 技术 Servlet是使用java servlet应用程 ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- tomcat URL乱码问题
用get传参时,显示乱码 在tomcat里的server.xml中添加一下即可. <Connector port="8080" protocol="HTTP/1.1 ...
- Android 支付宝以及微信支付快速接入流程
简介 随着移动支付的普及,越来越多的App采用第三发支付,在这里我们以支付宝为例,做一个快速集成! 一.Android快速实现支付宝支付 1.首先,我们需要前往支付宝开放平台,申请我们的支付功能:ht ...
- SQL Server order by语句学习回顾
主要学习: 1.以指定的次序返回查询结果 2.按多个字段排序 3.按字串排序 4.处理排序空值 5.根据数据项的键排序 具体实例1---以指定的次序返回查询结果 n使用ORDER BY子句可以对结果集 ...
- 【leetcode】Copy List with Random Pointer (hard)
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【剑指offer】题目20 顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出 ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- You know元音字母吗?
所谓元音字母,或者母音字母,就是语言里起着发声作用的字母.在英语中,A.E.I.O.U属于元音字母,其中U是半元音开音节和闭音节为数不多的5个元音字母看似简单,他们却能像变戏子一样跟辅音组合拼读成不同 ...
- iOS 没有安装对应客户端,不应显示对应的图标
现在很多APP为了让用户更加快捷方便注册,都会使用第三方进行登录,例如QQ/微信/淘宝等.但是上线审核被拒,大致会出现以下内容: Additionally, we found that your ap ...