首页 技术 正文
技术 2022年11月15日
0 收藏 597 点赞 2,382 浏览 1752 个字

题意:给你一串字符串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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848