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

题意:给定t组字符串每组m条询问——求问每条询问区间内有多少不同的子串。

题解:把每个询问区间的字符串hash一下存图,这样访问的复杂度就只有O(1).至于为什么不能用map查重我也不知道,用map+hash会超时。所以我们需要手动写个map(直接套用kuangbin大佬的模板)。最后只要利用二维前缀和即可输出答案。

Ac 代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<vector>
  6. #include<stack>
  7. #include<cstdio>
  8. #include<map>
  9. #include<set>
  10. #include<string>
  11. #include<queue>
  12. #define memt(a,b) memset(a,b,sizeof a)
  13. using namespace std;
  14. typedef unsigned long long ull;
  15. const int M = 1e4+7;
  16. const int maxn = 2e3+10;
  17. const int base = 13331;
  18. struct HASHMAP { //构造map,直接用map会tle(套用kuangbin大佬模板);
  19. int head[M],next[maxn],size;
  20. ull state[maxn];
  21. int f[maxn];
  22. void init(){
  23. size = 0;
  24. memt(head,-1);
  25. }
  26. int insert(ull val,int _id) {
  27. int h = val%M;
  28. for(int i = head[h]; i != -1; i = next[i])
  29. if(val == state[i]){
  30. int tmp = f[i];
  31. f[i] = _id;
  32. return tmp;
  33. }
  34. f[size] = _id;
  35. state[size] = val;
  36. next[size] = head[h];
  37. head[h] = size++;
  38. return 0;
  39. }
  40. } H;
  41. ull P[maxn]; //种子;
  42. ull S[maxn]; //存hash值;
  43. char str[maxn];
  44. int ans[maxn][maxn]; //存图查重;
  45. int main() {
  46. P[0] = 1;
  47. for(int i = 1; i < maxn; i++) P[i] = P[i-1] * base;
  48. int T;
  49. cin>>T;
  50. while(T--){
  51. scanf("%s",str);
  52. int n = strlen(str);
  53. S[0] = 0;
  54. for(int i = 1; i <= n; i++) S[i] = S[i-1]*base + str[i-1];
  55. memt(ans,0);
  56. for(int L = 1; L <= n; L++){
  57. H.init();
  58. for(int i = 1; i + L - 1 <= n; i++){
  59. //返回的是这个长度的串之前出现的位置,之前出现过,所以在那个位置到
  60. //当前这个位置这段区间的个数要减-1,同一个区间内同样的串只需要计算一次
  61. int l = H.insert(S[i+L-1] - S[i-1]*P[L],i);
  62. ans[i][i+L-1]++;
  63. ans[l][i+L-1]--;
  64. }
  65. }
  66. for(int i = n; i >= 0; i--)
  67. for(int j = i; j <= n; j++)
  68. ans[i][j] += ans[i+1][j] + ans[i][j-1] - ans[i+1][j-1];//二维前缀和;
  69. int m,u,v;
  70. cin>>m;
  71. while(m--){
  72. cin>>u>>v;
  73. cout<<ans[u][v]<<endl;
  74. }
  75. }
  76. return 0;
  77. }

hdu 4622 (hash+“map”)的更多相关文章

  1. 哈希表(Hash Map)

    今天第一次做Leetcode用到了散列表,之前学的数据结构的内容都忘了,正好趁热打铁补一补. 摘自其他博客的一个整合. 一.哈希表简介 数据结构的物理存储结构只有两种:顺序存储结构和链式存储结构(像栈 ...

  2. (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数

    原版 sorted [抄题]: [思维问题]: 存sum - nums[i](补集),若出现第二次则调出 [一句话思路]: hashmap中,重要的数值当做key,角标当做value. [画图]: [ ...

  3. D. Zero Quantity Maximization(hash+map)

    题意:就是让c=a*x+b,给你一个a[],b[],让你尽可能多的让c[]=0,输出有多少. 思路:直接令c=0,则x=-b/a, 也就是一条直线,通过这样就用hash值使相同的k值映射到一起,使用了 ...

  4. 【TOJ 1912】487-3279(hash+map)

    描述 Businesses like to have memorable telephone numbers. One way to make a telephone number memorable ...

  5. Reincarnation HDU - 4622 (后缀自动机)

    Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...

  6. 哈希表(Hash Table)/散列表(Key-Value)

    目录 1. 哈希表的基本思想 2. 哈希表的相关基本概念 1.概念: 2.哈希表和哈希函数的标准定义: 1)冲突: 2)安全避免冲突的条件: 3)冲突不可能完全避免 4)影响冲突的因素 3. 哈希表的 ...

  7. Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)

    Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...

  8. 【Unity Shader】六、使用法线贴图(Normal Map)的Shader

    学习资料: http://www.sikiedu.com/course/37/task/456/show# http://www.sikiedu.com/course/37/task/458/show ...

  9. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

随机推荐

  1. Eclipce怎么恢复误删类

    选择误删除文件在eclipse所在包(文件夹) 在包上单击右键. 选择restore from local history... 在弹出的对话框中选择需要恢复的文件

  2. Python Web Framework All In One

    Python Web Framework All In One Django and Flask are the top Python web frameworks so far. Django ht ...

  3. Battery API All In One

    Battery API All In One https://caniuse.com/?search=Battery navigator.getBattery() /* Promise {<pe ...

  4. cookie & session & token compare

    cookie & session & token compare cookie.session.token 区别和优缺点 存储位置 cookie 存在 client 端 session ...

  5. js double 精度损失 bugs

    js double 精度损失 bugs const arr = [ 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01 ]; // [ ...

  6. 微前端 & 微前端实践 & 微前端教程

    微前端 & 微前端实践 & 微前端教程 微前端 micro frontends https://micro-frontends.org/ https://github.com/neul ...

  7. py django 渲染前端打包的视图

    前端打包后基本这样 $ ls dist /static index.html 在index.html中的publicPath指向static 1. 创建一个www模块 $ python manage. ...

  8. redis和mysql结合数据一致性方案

    缓存读: 缓存由于高并发高性能,已经被广泛的应用.在读取缓存方面做法一致.流程如下: 写缓存: 1.先更新数据库,再更新缓存 2.先更新数据库,再删除缓存. (1).先更新数据库,再更新缓存 这套方案 ...

  9. MarkDown编辑器基础使用教程

    教程原创链接 MarkDown 段落和换行 一个 Markdown 段落是由一个或多个连续的文本行组成,它的前后要有一个以上的空行(空行的定义是显示上看起来像是空的,便会被视为空行.比方说,若某一行只 ...

  10. vue页面嵌套其他页面判断是否生产https

    if (location.protocol.indexOf('https') > -1) { var oMeta = document.createElement('meta'); oMeta. ...