hdu 4622 (hash+“map”)
题目链接: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”)的更多相关文章
- 哈希表(Hash Map)
今天第一次做Leetcode用到了散列表,之前学的数据结构的内容都忘了,正好趁热打铁补一补. 摘自其他博客的一个整合. 一.哈希表简介 数据结构的物理存储结构只有两种:顺序存储结构和链式存储结构(像栈 ...
- (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数
原版 sorted [抄题]: [思维问题]: 存sum - nums[i](补集),若出现第二次则调出 [一句话思路]: hashmap中,重要的数值当做key,角标当做value. [画图]: [ ...
- D. Zero Quantity Maximization(hash+map)
题意:就是让c=a*x+b,给你一个a[],b[],让你尽可能多的让c[]=0,输出有多少. 思路:直接令c=0,则x=-b/a, 也就是一条直线,通过这样就用hash值使相同的k值映射到一起,使用了 ...
- 【TOJ 1912】487-3279(hash+map)
描述 Businesses like to have memorable telephone numbers. One way to make a telephone number memorable ...
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- 哈希表(Hash Table)/散列表(Key-Value)
目录 1. 哈希表的基本思想 2. 哈希表的相关基本概念 1.概念: 2.哈希表和哈希函数的标准定义: 1)冲突: 2)安全避免冲突的条件: 3)冲突不可能完全避免 4)影响冲突的因素 3. 哈希表的 ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...
- 【Unity Shader】六、使用法线贴图(Normal Map)的Shader
学习资料: http://www.sikiedu.com/course/37/task/456/show# http://www.sikiedu.com/course/37/task/458/show ...
- Bomb HDU - 3555 (数位DP)
Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...
随机推荐
- Eclipce怎么恢复误删类
选择误删除文件在eclipse所在包(文件夹) 在包上单击右键. 选择restore from local history... 在弹出的对话框中选择需要恢复的文件
- 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 ...
- Battery API All In One
Battery API All In One https://caniuse.com/?search=Battery navigator.getBattery() /* Promise {<pe ...
- cookie & session & token compare
cookie & session & token compare cookie.session.token 区别和优缺点 存储位置 cookie 存在 client 端 session ...
- 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 ]; // [ ...
- 微前端 & 微前端实践 & 微前端教程
微前端 & 微前端实践 & 微前端教程 微前端 micro frontends https://micro-frontends.org/ https://github.com/neul ...
- py django 渲染前端打包的视图
前端打包后基本这样 $ ls dist /static index.html 在index.html中的publicPath指向static 1. 创建一个www模块 $ python manage. ...
- redis和mysql结合数据一致性方案
缓存读: 缓存由于高并发高性能,已经被广泛的应用.在读取缓存方面做法一致.流程如下: 写缓存: 1.先更新数据库,再更新缓存 2.先更新数据库,再删除缓存. (1).先更新数据库,再更新缓存 这套方案 ...
- MarkDown编辑器基础使用教程
教程原创链接 MarkDown 段落和换行 一个 Markdown 段落是由一个或多个连续的文本行组成,它的前后要有一个以上的空行(空行的定义是显示上看起来像是空的,便会被视为空行.比方说,若某一行只 ...
- vue页面嵌套其他页面判断是否生产https
if (location.protocol.indexOf('https') > -1) { var oMeta = document.createElement('meta'); oMeta. ...