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

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

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

Ac 代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
#define memt(a,b) memset(a,b,sizeof a)
using namespace std;
typedef unsigned long long ull;
const int M = 1e4+7;
const int maxn = 2e3+10;
const int base = 13331;
struct HASHMAP { //构造map,直接用map会tle(套用kuangbin大佬模板);
int head[M],next[maxn],size;
ull state[maxn];
int f[maxn];
void init(){
size = 0;
memt(head,-1);
}
int insert(ull val,int _id) {
int h = val%M;
for(int i = head[h]; i != -1; i = next[i])
if(val == state[i]){
int tmp = f[i];
f[i] = _id;
return tmp;
}
f[size] = _id;
state[size] = val;
next[size] = head[h];
head[h] = size++;
return 0;
}
} H;
ull P[maxn]; //种子;
ull S[maxn]; //存hash值;
char str[maxn];
int ans[maxn][maxn]; //存图查重;
int main() {
P[0] = 1;
for(int i = 1; i < maxn; i++) P[i] = P[i-1] * base;
int T;
cin>>T;
while(T--){
scanf("%s",str);
int n = strlen(str);
S[0] = 0;
for(int i = 1; i <= n; i++) S[i] = S[i-1]*base + str[i-1];
memt(ans,0);
for(int L = 1; L <= n; L++){
H.init();
for(int i = 1; i + L - 1 <= n; i++){
//返回的是这个长度的串之前出现的位置,之前出现过,所以在那个位置到
//当前这个位置这段区间的个数要减-1,同一个区间内同样的串只需要计算一次
int l = H.insert(S[i+L-1] - S[i-1]*P[L],i);
ans[i][i+L-1]++;
ans[l][i+L-1]--;
}
}
for(int i = n; i >= 0; i--)
for(int j = i; j <= n; j++)
ans[i][j] += ans[i+1][j] + ans[i][j-1] - ans[i+1][j-1];//二维前缀和;
int m,u,v;
cin>>m;
while(m--){
cin>>u>>v;
cout<<ans[u][v]<<endl;
}
}
return 0;
}

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. 2019牛客多校第一场I Points Division(DP)题解

    题意: n个点,分成两组A,B,如果点i在A中,那么贡献值\(a_i\),反之为\(b_i\). 现要求任意\(i \in A,j \in B\)不存在 \(x_i >= x_j\) 且 \(y ...

  2. 深入理解JavaScript中的箭头

    箭头函数可以使我们的代码更加简洁,如下: var sum = (a,b) => a+b; JavaScript 充满了我们需要编写在其他地方执行的小函数的情况. 例如: arr.forEach( ...

  3. 使用 js 实现一个简易版的 async 库

    使用 js 实现一个简易版的 async 库 具有挑战性的前端面试题 series & parallel 串行,并行 refs https://www.infoq.cn/article/0NU ...

  4. The Weekly Web Dev Challenge: Emoji Ratings

    The Weekly Web Dev Challenge: Emoji Ratings /* DESCRIPTION: You job is to enable users to give a rat ...

  5. Twitter 分享

    Twitter 分享 Twitter Share API https://twitter.com/intent/tweet?url= &text= demo ?url= https://www ...

  6. Learning CSS with Chrome DevTools

    Learning CSS with Chrome DevTools CSS 复合属性展开 border background box-shadow flex-flow flex HTML5 custo ...

  7. bob and brad physical therapy knee exercise

    bob and brad physical therapy knee exercise 鲍勃和布拉德物理治疗膝关节运动 https://bobandbrad.com/ youtube https:// ...

  8. iframe 父子互传消息,父页面滚动,子页面触发父页面高度

    https://blog.csdn.net/qq_38366657/article/details/81538145 // 父页面的js<iframe id='TopHeader' src=&q ...

  9. vue常用方法封装-一键安装使用(赠送免费工具)

    相信大家在使用vue开发过程中一定遇到了各种方法的整理收集,每次遇到新的问题都需要找到合适的方法 这里我给大家封装了一些vue项目中常用到的方法合集,免费提供费大家 因此,jsoften横空出世,不为 ...

  10. 【转】ICP算法(Iterative Closest Point迭代最近点算法)

    原文网址:https://www.cnblogs.com/sddai/p/6129437.html.转载主要方便随时可以查看,如有版权要求请及时联系. 最近在做点云匹配,需要用c++实现ICP算法,下 ...