题意:给你一串字符串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)的更多相关文章

  1. HDU 4821 String(BKDRHash)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给出一个字符串,现在问你可以找出多少个长度为M*L的子串,该子串被分成L个段,并且每个段的字符串都是 ...

  2. 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 ...

  3. HDU 4821 String(2013长春现场赛I题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...

  4. HDU 4821 String 字符串hash

    String Problem Description   Given a string S and two integers L and M, we consider a substring of S ...

  5. HDU 2523 sort (hash)

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...

  6. HDU - 1496 Equations (hash)

    题意: 多组测试数据. 每组数据有一个方程 a*x1^2 + b*x2^2 + c*x3^2 + d*x4^2 = 0,方程中四个未知数 x1, x2, x3, x4 ∈ [-100, 100], 且 ...

  7. Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)

    help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...

  8. 哈希(Hash)与加密(Encrypt)相关内容

    1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...

  9. 【Redis】命令学习笔记——哈希(hash)(15个超全字典版)

    本篇基于redis 4.0.11版本,学习哈希(hash)相关命令. hash 是一个string类型的field和value的映射表,特别适合用于存储对象. 序号 命令 描述 实例 返回 HSET ...

随机推荐

  1. 初识 MySQL 5.6 新特性、功能

    背景: 之前介绍过 MySQL 5.5 新功能.参数,现在要用MySQL5.6,所以就学习和了解下MySQL5.6新的特性和功能,尽量避免踩坑.在后续的学习过程中文章也会不定时更新. 一:参数默认值的 ...

  2. We7 CMS研究

    我下载的we7 3.0是基于vs 2010的,官方网站也建议使用vs2010,但是我有追新的习惯,并相信vs 2013一定能够兼容vs2010的项目,于是在vs2013下打开解决方案并且全部升级,把目 ...

  3. codeforces 507B. Painting Pebbles 解题报告

    题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...

  4. EF的各种删除方法

    //2.1检查 id 是否存在 //2.2执行删除 Models.Student stu = new Models.Student() { Id = id }; //db.Students.Attac ...

  5. 模板类重载<<运算符

    写了一个Matrix模板类,需要重载<<, 1.需要友元函数 2.需要此函数的实现在.h中(本人试验出来的,放在.cpp中编译不通过) template <typename T> ...

  6. VB.NET 注册表基本操作

    ''' <summary> ''' 注册表设置值 ''' </summary> ''' <param name="strKey"></pa ...

  7. supersr--addSubview和 insertSubView 区别

    A addSubview B  是将B直接覆盖在A的最上层  例子: [self.view addSubview:scrollView]; A insertSubView B AtIndex:2 是将 ...

  8. October 2nd 2016 Week 41st Sunday

    The road to success is lined with many tempting parking spaces. 通往成功的路边充斥着许多诱人的休息区. Exhausted, I thi ...

  9. mybatis 学习!

    参考链接 http://www.mybatis.org/spring/zh/mappers.html http://www.cnblogs.com/fangjian0423/p/spring-myba ...

  10. Mybatis 字符绑定

    http://blog.csdn.net/softwarehe/article/details/8889206