题目链接: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. HDU 3681 Prison Break(状压DP + BFS)题解

    题意:一张图,F是起点,Y是必须要到的点,D不能走,G可以充电.可以往四个方向走,每走一步花费一个电,走到G可以选择充满电或者不充,每个G只能充一次.问你走遍Y的最小初始点亮.number(G) + ...

  2. 翻译:《实用的Python编程》01_05_Lists

    目录 | 上一节 (1.4 字符串) | 下一节 (1.6 文件) 1.5 列表 本节介绍 Python 原始数据类型列表(list). 列表是一种有序的集合. 创建列表 使用方括号 [] 来定义列表 ...

  3. 阿里巴巴java开发手册(2020版)

    阿里巴巴java开发手册(2020版) 2020版 链接: pan.baidu.com/s/1Zls_FUBK- 密码: titz 2019版 链接: pan.baidu.com/s/1cvCVQvj ...

  4. BB link

    1 1 1 BB link: 1 1 demo: code: result: 1 1 1 1 1 1 1

  5. how to share UI components

    how to share UI components The shared component cloud · Bit https://bit.dev/ A better way to build w ...

  6. SQL Tutorials & MySQL & SQL Server

    SQL Tutorials SQL MySQL https://www.mysql.com/ $ mysql --version # mysql Ver 8.0.21 for osx10.15 on ...

  7. PPT & order & animation

    PPT & order & animation powerpoint order of appearance https://support.office.com/en-us/arti ...

  8. Nestjs mongodb

    nestjs 文档 mongoose 文档 使用"@meanie/mongoose-to-json"转换查询后返回的json数据 将"_id"转为"i ...

  9. ng 基础

    文档 组件的工作只管用户体验,而不用顾及其它. 它应该提供用于数据绑定的属性和方法,以便作为视图和应用逻辑的中介者 组件应该把诸如从服务器获取数据.验证用户输入或直接往控制台中写日志等工作委托给各种服 ...

  10. 大胆预计SPC算力空投收益,月收益22.8%

    此前,NGK官方公告表示,NGK算力持有者获得SPC的数量是根据200万枚SPC除以全网算力总量决定的. 举个例子,假设全网算力总量为500万,那么每个算力持有者如果持有一个算力,则可获得200万÷5 ...