题目链接https://vjudge.net/problem/HDU-4821

题意:给定字符串S ,询问用几个子串满足 :

1.长度为n*len  。

2. n个子串都不相同。

题解:倒序hash将S第i位的字符变成ull,利用map维护每个子串,遍历的时候只需要去掉开头小串然后加上后面一个小串就可以实现整个字符的遍历。

Ac 代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<map>
  6. using namespace std;
  7. const int M=1e5+5;
  8. typedef unsigned long long ull;
  9. ull has[M],l[M],tmp;
  10. map<ull,int>mp;
  11. char str[M];
  12. int n,len,ans;
  13. int base=31;
  14. int main(){
  15. l[0]=1;
  16. for(int i=1;i<M;i++) l[i]=l[i-1]*base;
  17. while(~scanf("%d %d",&n,&len)){
  18. scanf("%s",str);
  19. int len1=strlen(str);
  20. has[len1]=0;
  21. for(int i=len1-1;i>=0;i--){
  22. has[i]=has[i+1]*base+str[i]-'a'+1; //将第i为以后的字符串变成ull值;
  23. }
  24. ans=0;
  25. for(int i=0;i<len&&i+n*len<=len1;i++){ //对每一个长度为 n*len的子串进行判断;
  26. mp.clear();
  27. for(int j=i;j<i+n*len;j+=len){ //判断该 i+n*len 段子串是否符合每个子串都不同的条件;
  28. tmp=has[j]-has[j+len]*l[len];
  29. mp[tmp]++;
  30. }
  31. if(mp.size()==n) ans++; //符合种类加一;
  32. for(int j=i+n*len;j+len<=len1;j+=len){ //以len为单位每次划出一个len 在末尾加上一个len
  33. tmp=has[j-n*len]-has[j-(n-1)*len]*l[len]; // 如果此时还满足mp.size()==n ans++;
  34. mp[tmp]--;
  35. if(mp[tmp]==0) mp.erase(tmp);
  36. tmp=has[j]-has[j+len]*l[len];
  37. mp[tmp]++;
  38. if(mp.size()==n) ans++;
  39. }
  40. }
  41. cout<<ans<<endl;
  42. }
  43. return 0;
  44. }

Hdu 4821 (字符串hash+map)的更多相关文章

  1. hdu 4821 字符串hash+map判重 String (长春市赛区I题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...

  2. HDU 4821 字符串hash

    题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...

  3. (通俗易懂小白入门)字符串Hash+map判重——暴力且优雅

    字符串Hash 今天我们要讲解的是用于处理字符串匹配查重的一个算法,当我们处理一些问题如给出10000个字符串输出其中不同的个数,或者给一个长度100000的字符串,找出其中相同的字符串有多少个(这样 ...

  4. hdu 4622 (hash+“map”)

    题目链接:https://vjudge.net/problem/HDU-4622 题意:给定t组字符串每组m条询问--求问每条询问区间内有多少不同的子串. 题解:把每个询问区间的字符串hash一下存图 ...

  5. 2013 Asia Regional Changchun I 题,HDU(4821),Hash

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...

  6. HDU 4821 String hash

    String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. hdu 1880 字符串hash

    /*普通的hsah 由于元素太多 空间很小..hash碰撞很厉害.30分*/ #include<iostream> #include<cstdio> #include<c ...

  8. HDU 1880 字符串hash 入门题

    Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...

  9. Hdu 1800 字符串hash

    题目链接 题意: 给出n(n<=3000)个字符串(长度<30,数字组成,肯能存在前导0), 问该序列最少可以分成多少个单调序列.可以转化成求相同字符串的个数的最大值 附上代码: /*** ...

随机推荐

  1. 常用SQL语句1-增删改查

    一.名词解释 RDBMS 即关系数据库管理系统(Relational Database Management System)的特点: 1.数据以表格的形式出现 2.每行为各种记录名称 3.每列为记录名 ...

  2. 『数据结构与算法』二叉查找树(BST)

    微信搜索:码农StayUp 主页地址:https://gozhuyinglong.github.io 源码分享:https://github.com/gozhuyinglong/blog-demos ...

  3. js Nullish Coalescing Operator

    js Nullish Coalescing Operator 空值合并 const n = null ?? 'default string'; console.log(n); // "def ...

  4. 如何强制删除 baidu/tempdata/con.dat 的垃圾文件! How to fix locked SD card: 读卡器 损坏,补救措施!

    https://www.youtube.com/watch?v=y2c37dcxNto&feature=youtu.be 使用windows command prompt 强制删除 baidu ...

  5. TypeScript constructor public cause duplicate bug

    TypeScript constructor public cause duplicate bug constructor public const log = console.log; // con ...

  6. 前端 vs 后端

    前端 vs 后端 前端与后端: 有什么区别? 前端和后端是计算机行业中最常用的两个术语. 在某种程度上,它们成了流行语. 它们决定了您作为软件开发人员所从事的工作类型,所使用的技术以及所获得的收入. ...

  7. DNS & TXT recode & Google Search Console

    DNS & TXT recode & Google Search Console domain name verification / 域名验证 demo DNS TXT recode ...

  8. js assert

    js assert console.assert The console.assert() method writes an error message to the console if the a ...

  9. taro 三端开发

    taro 三端开发 wx 小程序, alipay 小程序,H5 https://taro-docs.jd.com/taro/docs/GETTING-STARTED.html#h5 https://t ...

  10. css命名规范和书写规范

    1.位置属性(position, top, right, z-index, display, float等)2.大小(width, height, padding, margin)3.文字系列(fon ...