哈希
tomjobs
别慌,慌也没用
展开
-
LeetCode 706. 设计哈希映射(数组法,稀疏矩阵法,链地址法,开放定址法)
思路:参考:https://leetcode-cn.com/problems/design-hashmap/solution/706-she-ji-ha-xi-ying-she-by-jyj407-nzcz/解法1:因为数据不大,直接用数组保存每个值的映射。class MyHashMap {private: vector<int>cache;public: /** Initialize your data structure here. */ MyHashM.原创 2021-02-25 15:15:15 · 194 阅读 · 0 评论 -
LeetCode 381. O(1) 时间插入、删除和获取随机元素 - 允许重复(链表,哈希套哈希)
思路:这道题:“LeetCode380. 常数时间插入、删除和获取随机元素”的加强版。同样的用vectorvectorvector加哈希记录每个值的位置。因为可能出现重复的元素,所以我们用哈希套哈希,来记录这个值对应所有出现的位置。class RandomizedCollection {private: vector<int>vec; unordered_map<int,unordered_map<int,int>>vec_table;publ.原创 2021-02-24 22:46:12 · 142 阅读 · 0 评论 -
LeetCode380. 常数时间插入、删除和获取随机元素(哈希+vector)
思路:字节面试的时候问过这种类型的题,我答的不是很好。O(1)插入、删除?第一想法就是链表,事实上我最一开始写的就是链表+哈希,然后用哈希记录每个值对应的迭代器。然后对于getrandom的操作,我的想法是用一个迭代器记录当前指向,每getrandom操作一次,迭代器右移(右移到end后回到起点)。不过貌似随机性不够强,评测姬不认可我的结果。。。最后学习了一下题解的写法,用的是vectorvectorvector加上哈希,哈希记录的是每个值的位置。确实以前没见过这种写法,因为我的认知就是vector.原创 2021-02-24 22:14:07 · 181 阅读 · 0 评论 -
LeetCode 460. LFU 缓存(链表+哈希)
思路:链表+哈希,类似LRU的构造。代码参考自leetcode官方题解。struct Node { int key,val,freq; Node(int _key,int _val,int _freq):key(_key),val(_val),freq(_freq){}};class LFUCache {public: LFUCache(int capacity) { minfreq = 0; cap = capacity; .原创 2021-02-24 20:45:13 · 133 阅读 · 0 评论 -
Codeforces-1471 D. Strange Definition(哈希,质因数分解)
Let us call two integers ???? and ???? adjacent if ????????????(????,????)????????????(????,????) is a perfect square. For example, 3 and 12 are adjacent, but 6 and 9 are not.Here ????????????(????,????) denotes the greatest common divisor (GCD) of intege原创 2021-01-17 17:38:10 · 284 阅读 · 0 评论 -
LeetCode 454. 四数相加 II(哈希)
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。例如:输入:A = [ 1, 2]B = [-2,-1]C = [-1, 2]D = [ 0, 2]输出:2解释:两个元组如下:原创 2021-01-07 11:42:22 · 252 阅读 · 0 评论 -
2019湖南省赛 C.Distinct Substrings(哈希+二分,扩展KMP)
思路:串的最后加上i以后,实际上加上了n+1n+1n+1个串,我们要减去重复的子串个数。假设串加上iii后,对于长度为midmidmid的后缀发生重复,那么对于长度为1...mid−11...mid-11...mid−1的后缀也会发生重复,所以要判断有midmidmid个重复子串,实际就是判断新串长度为midmidmid的后缀和旧串某个长度为midmidmid的后缀发生重复。这个过程具有单调性,所以可以哈希+二分写。#include <cstdio>#include <cstri.原创 2020-11-11 21:08:44 · 183 阅读 · 0 评论 -
2020小米网络赛第二场 Shift and Reverse(哈希)
思路:经过模拟,发现就是将序列前面一部分放到后面去,或者将序列反转后,再将前面一部分放到后面。所以直接序列扩展一倍每次取长度为nnn就得到所有结果了,用hash判断出现了多少个。#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<map>#i.原创 2020-11-08 11:36:04 · 146 阅读 · 0 评论 -
2020CCPC威海 G Caesar Cipher(线段树+哈希)
Have you ever heard of the Caesar cipher? It is one of the simplest and best-known encryption techniques. Named after Julius Caesar, he used this cipher to communicate with his generals.Caesar cipher is a type of substitution cipher in which each letter i原创 2020-10-30 17:46:47 · 347 阅读 · 0 评论 -
2020杭电多校第二场 The Oculus(哈希,斐波那契数列)
Problem DescriptionLet’s define the Fibonacci sequence F1,F2,… as F1=1,F2=2,Fi=Fi−1+Fi−2 (i≥3).It’s well known that every positive integer x has its unique Fibonacci representation (b1,b2,…,bn) such that:· b1×F1+b2×F2+⋯+bn×Fn=x.· bn=1, and for each i (原创 2020-07-25 20:39:38 · 386 阅读 · 0 评论 -
A.All with Pairs(Hash+Kmp) 2020牛客暑期多校训练营(第二场)
题目都很短就懒得写题意了。思路:把每个字符的后缀都用hash表示然后用map存起来算数目。统计的时候,对于当前的前缀我们可以算出其在后缀中出现的次数。但问题是这样可能有重复。解决办法是:cnt[next[i]]−=cnt[i]cnt[next[i]] -= cnt[i]cnt[next[i]]−=cnt[i]因为假设p1是p2的最长公共前后缀,那么p2对于出现的后缀,被p1对应出现的后缀包含,且不能用,所以减掉。至于为什么只需要找最长公共前后缀,而不需要找到所有将p1作为后缀的串,是因为如果.原创 2020-07-14 16:40:17 · 242 阅读 · 0 评论 -
51nod1055 最长等差数列(区间dp,哈希,双指针)
N个不同的正整数,找出由这些数组成的最长的等差数列。例如:1 3 5 6 8 9 10 12 13 14等差子数列包括(仅包括两项的不列举)1 3 51 5 9 133 6 9 123 8 135 9 136 8 10 12 14其中6 8 10 12 14最长,长度为5。输入第1行:N,N为正整数的数量(3 <= N <= 10000)。第2 - N+1行:N...原创 2020-04-11 22:32:09 · 250 阅读 · 0 评论 -
ICPC Central Europe Regional Contest 2019 A. ABB(马拉车,哈希)
Fernando was hired by the University of Waterloo to finish a development project the university started some time ago. Outside the campus, the university wanted to build its representative bungalow street for important foreign visitors and collaborators.C原创 2020-06-21 20:40:18 · 362 阅读 · 0 评论 -
Extend KMP HihoCoder - 1084(二分+hash)
Do you know KMP? It is used to judge whether a string is a substring of another string. Today we want to extend it.In information theory, the Hamming distance between two strings of equal length is t...原创 2020-01-28 16:51:37 · 277 阅读 · 0 评论 -
BZOJ3098. Hash Killer II(生日攻击)
Description这天天气不错,hzhwcmhf神犇给VFleaKing出了一道题:给你一个长度为N的字符串S,求有多少个不同的长度为L的子串。子串的定义是S[l]、S[l + 1]、… S[r]这样连续的一段。两个字符串被认为是不同的当且仅当某个位置上的字符不同。VFleaKing一看觉得这不是Hash的裸题么!于是果断写了哈希 + 排序。而hzhwcmhf神犇心里自然知道,这题...原创 2020-01-27 22:03:21 · 324 阅读 · 0 评论 -
51nod1089 最长回文子串 V2(Manacher算法/哈希)
回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。输入一个字符串Str,输出Str里最长回文子串的长度。输入输入Str(Str的长度 <= 100000)输出输出最长回文子串的长度L。输入样例daabaac输出样例5知乎上 https://www.zhihu.com/question/37289584 ”c加加编程思想“ 答主的回答很棒。思路:...原创 2020-01-25 23:37:43 · 340 阅读 · 0 评论 -
Constellations POJ - 3690(二维哈希,寻找矩阵)
The starry sky in the summer night is one of the most beautiful things on this planet. People imagine that some groups of stars in the sky form so-called constellations. Formally a constellation is a ...原创 2019-10-28 16:57:41 · 429 阅读 · 0 评论 -
Sumsets POJ - 2549(折半搜索 哈希表)
Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.InputSeveral S, each consisting of a line containing an integer 1 <= n <=...原创 2019-10-28 00:20:23 · 283 阅读 · 0 评论 -
Musical Theme POJ - 1743(后缀数组+二分/HASH + 二分)
A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1…88, each representing a key on the piano. It is unfortunate but true that this representati...原创 2019-10-27 20:43:46 · 202 阅读 · 0 评论 -
Milk Patterns POJ - 3261(后缀数组+二分/HASH + 二分)
Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can’t predict the quality of milk from one day to th...原创 2019-10-27 21:33:15 · 201 阅读 · 0 评论 -
139. 回文子串的最大长度(哈希)
如果一个字符串正着读和倒着读是一样的,则称它是回文的。给定一个长度为N的字符串S,求他的最长回文子串的长度是多少。输入格式输入将包含最多30个测试用例,每个测试用例占一行,以最多1000000个小写字符的形式给出。输入以一个以字符串“END”(不包括引号)开头的行表示输入终止。输出格式对于输入中的每个测试用例,输出测试用例编号和最大回文子串的长度(参考样例格式)。每个输出占一行。输...原创 2019-08-20 16:14:57 · 496 阅读 · 0 评论 -
ACWING137. 雪花雪花雪花(哈希表)
有N片雪花,每片雪花由六个角组成,每个角都有长度。第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,…,ai,6。因为雪花的形状是封闭的环形,所以从任何一个角开始顺时针或逆时针往后记录长度,得到的六元组都代表形状相同的雪花。例如ai,1,ai,2,…,ai,6和ai,2,ai,3,…,ai,6,ai,1就是形状相同的雪花。ai,1,ai,2,…,ai,6和ai,6,ai,...原创 2019-08-20 15:08:22 · 636 阅读 · 0 评论 -
138. 兔子与兔子(哈希)
很久很久以前,森林里住着一群兔子。有一天,兔子们想要研究自己的 DNA 序列。我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母)。然后我们每次选择两个区间,询问如果用两个区间里的 DNA 序列分别生产出来两只兔子,这两个兔子是否一模一样。注意两个兔子一模一样只可能是他们的 DNA 序列一模一样。输入格式第一行输入一个 DNA 字符串...原创 2019-08-20 13:52:15 · 485 阅读 · 0 评论