Hdu 4821 (字符串hash+map)
题目链接https://vjudge.net/problem/HDU-4821
题意:给定字符串S ,询问用几个子串满足 :
1.长度为n*len 。
2. n个子串都不相同。
题解:倒序hash将S第i位的字符变成ull,利用map维护每个子串,遍历的时候只需要去掉开头小串然后加上后面一个小串就可以实现整个字符的遍历。
Ac 代码:
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int M=1e5+5;
typedef unsigned long long ull;
ull has[M],l[M],tmp;
map<ull,int>mp;
char str[M];
int n,len,ans;
int base=31;
int main(){
l[0]=1;
for(int i=1;i<M;i++) l[i]=l[i-1]*base;
while(~scanf("%d %d",&n,&len)){
scanf("%s",str);
int len1=strlen(str);
has[len1]=0;
for(int i=len1-1;i>=0;i--){
has[i]=has[i+1]*base+str[i]-'a'+1; //将第i为以后的字符串变成ull值;
}
ans=0;
for(int i=0;i<len&&i+n*len<=len1;i++){ //对每一个长度为 n*len的子串进行判断;
mp.clear();
for(int j=i;j<i+n*len;j+=len){ //判断该 i+n*len 段子串是否符合每个子串都不同的条件;
tmp=has[j]-has[j+len]*l[len];
mp[tmp]++;
}
if(mp.size()==n) ans++; //符合种类加一;
for(int j=i+n*len;j+len<=len1;j+=len){ //以len为单位每次划出一个len 在末尾加上一个len
tmp=has[j-n*len]-has[j-(n-1)*len]*l[len]; // 如果此时还满足mp.size()==n ans++;
mp[tmp]--;
if(mp[tmp]==0) mp.erase(tmp);
tmp=has[j]-has[j+len]*l[len];
mp[tmp]++;
if(mp.size()==n) ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
Hdu 4821 (字符串hash+map)的更多相关文章
- hdu 4821 字符串hash+map判重 String (长春市赛区I题)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...
- HDU 4821 字符串hash
题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...
- (通俗易懂小白入门)字符串Hash+map判重——暴力且优雅
字符串Hash 今天我们要讲解的是用于处理字符串匹配查重的一个算法,当我们处理一些问题如给出10000个字符串输出其中不同的个数,或者给一个长度100000的字符串,找出其中相同的字符串有多少个(这样 ...
- hdu 4622 (hash+“map”)
题目链接:https://vjudge.net/problem/HDU-4622 题意:给定t组字符串每组m条询问--求问每条询问区间内有多少不同的子串. 题解:把每个询问区间的字符串hash一下存图 ...
- 2013 Asia Regional Changchun I 题,HDU(4821),Hash
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...
- HDU 4821 String hash
String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 1880 字符串hash
/*普通的hsah 由于元素太多 空间很小..hash碰撞很厉害.30分*/ #include<iostream> #include<cstdio> #include<c ...
- HDU 1880 字符串hash 入门题
Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...
- Hdu 1800 字符串hash
题目链接 题意: 给出n(n<=3000)个字符串(长度<30,数字组成,肯能存在前导0), 问该序列最少可以分成多少个单调序列.可以转化成求相同字符串的个数的最大值 附上代码: /*** ...
随机推荐
- tensorflow加载ckpt出错
Issue链接 问题: tensorflow加载ckpt出错 此处原因: 该ckpt文件对应的tensorflow版本过老, 其中的部分内置变量名发生了改变. 提示: Key lstm_o/bidir ...
- Python errors All In One
Python errors All In One SyntaxError: invalid character in identifier \u200b, ZERO WIDTH SPACE https ...
- Lua 从入门到放弃
Lua 从入门到放弃 What is Lua? Lua is a powerful, efficient, lightweight, embeddable scripting language. It ...
- 微软收购 GitHub
微软收购 GitHub 微软收购 GitHub震惊业界:引发开发者信任问题 https://news.cnblogs.com/n/598432/ GitLab refs xgqfrms 2012-20 ...
- 图解 git workflow
图解 git workflow 图解 git 工作流 git-flow https://www.git-tower.com/learn/git/ebook/cn/command-line/advanc ...
- how to import a SQL file in MySQL command line
how to import a SQL file in MySQL command line execute .sql file, macOS $mysql> source \home\user ...
- Next.js & SSR & CSR & SG
Next.js & SSR & CSR & SG getStaticPaths, getStaticProps, getServerSideProps getStaticPro ...
- taro taro 多端同步调试
taro 多端同步调试 debug https://nervjs.github.io/taro/docs/envs-debug.html
- nasm astrcspn函数 x86
xxx.asm %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export ast ...
- nasm win x86 hello world
hello.asm: extern MessageBoxA extern ExitProcess section .data title db "caption.",0 messa ...