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 ...
随机推荐
- ACM/ICPC 之 DP-整数划分问题初探 (POJ1221)
写下这道题的原因很简单= =,因为这一题的状态转移方程不好找,另一方面,我看到很多针对这一题写的解题报告都把累加状态说得模棱两可,甚至直接说成了一个单一状态,弄得本是菜鸟的我硬生生折磨了一上午画了几个 ...
- Innodb 表空间卸载、迁移、装载
从MySQL的Innodb特性中我们知道,Inndob的表空间有共享和独享的特点,如果是共享的.则默认会把表空间存放在一个文件中(ibdata1),当开启独享表空间参数Innodb_file_per_ ...
- nyoj113_字符串替换
字符串替换 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 编写一个程序实现将字符串中的所有"you"替换成"we" 输入 ...
- 【python】id()函数
来源:百度知道 >>> a=2.0 >>> b=2.0 >>> id(a) 524440880 >>> id(b) 524440 ...
- Jquery的鼠标移动上去显示div,鼠标离开的时候隐藏div效果
有时候我们需要这个效果:当鼠标放上去的时候显示一个div,当鼠标移走的时候就将div隐藏了.代码如下,记得引入Jquyer库.(当鼠标移动到id=menu的div上的时候,显示id=list的div, ...
- [Linux] vim的高亮查找操作
reference : http://blog.chinaunix.net/uid-20732478-id-763411.html 使用了VIM这么久,却一直无法牢记一些基本的操作指令.今天查找一个 ...
- sehll_if
-s file 文件大小非0时为真 [ -f "somefile" ] :判断是否是一个文件[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可 ...
- NYOJ题目770仿射密码
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs4AAAIUCAIAAACFKz0yAAAgAElEQVR4nO3dPXLruLaG4TsJ5RqIYw
- 什么是DMI,SMBIOS,符合SMBIOS规范的计算机的系统信息获取方法
转自:http://www.cnblogs.com/gunl/archive/2011/08/08/2130719.html DMI是英文单词Desktop Management Interface的 ...
- 【翻译十九】-java之执行器
Executors In all of the previous examples, there's a close connection between the task being done by ...