多学习。

随笔分类 -  数据结构【算法题】

包括单链表,双链表,栈,队列,单调栈,单调队列,KMP,Trie,并查集,堆,哈希表等内容。
摘要:AcWing 143. 最大异或对 题解 将每个数的二进制形式存入Trie树,再对每个数的每一位进行遍历,若为1则找0,若为0则找1。 #include <iostream> using namespace std; const int N = 1e5 + 10, M = 3100010; //每个 阅读全文
posted @ 2022-05-21 22:55 czyaaa 阅读(25) 评论(0) 推荐(0) 编辑
摘要:AcWing841. 字符串哈希 题解 前缀字符串哈希 p = 131 或 13331 Q = 2^64 百分之九十九概率不会出现冲突 #include <iostream> using namespace std; const int N = 1e5 + 10, P = 131; typedef 阅读全文
posted @ 2022-05-21 11:04 czyaaa 阅读(28) 评论(0) 推荐(0) 编辑
摘要:AcWing840. 模拟散列表 题解 哈希函数:将题目输入的数映入成0~10^5范围的下标存入数组。 哈希取模的数最好是质数,这样冲突的概率最小(数学上存在证明,感兴趣可去搜) 拉链法 #include <iostream> #include <cstdio> #include <cstring> 阅读全文
posted @ 2022-05-20 22:43 czyaaa 阅读(145) 评论(0) 推荐(0) 编辑
摘要:AcWing839. 模拟堆 题解 #include <iostream> using namespace std; const int N = 1e5+10; //hp:堆中下标k,集合中下标为hp[k] //ph:集合中下标k,堆中下标ph[k] int p[N], h[N], hp[N], p 阅读全文
posted @ 2022-05-20 21:30 czyaaa 阅读(56) 评论(0) 推荐(0) 编辑
摘要:堆 (下标一定要从0, 0无法乘2) 该讲解的是小根堆,如果是大根堆,则需转换一下思维 堆是一颗完全二叉树,且最后一层是从左往右存储的,堆每个父节点都小于等于左右结点,故根节点为最小值 堆有两个操作,其余所有操作都可由这两个操作组合而成: down(x):当x结点的值变大的时候,我们要将x向下移动, 阅读全文
posted @ 2022-05-20 17:24 czyaaa 阅读(63) 评论(0) 推荐(0) 编辑
摘要:AcWing240. 食物链 题解 (2->1代表1吃2) 轻易可知,该食物链成一个环,只要得知该环中两条边,即可推出第三条边 将所有点存储在一个集合中,集合的边表示结点之间的关系,利用点到根节点的距离判断种类,故我们需要额外维护一个保存到父节点距离的数组。 路径压缩:还是将父节点直接改为根节点,距 阅读全文
posted @ 2022-05-19 12:02 czyaaa 阅读(43) 评论(0) 推荐(0) 编辑
摘要:AcWing837. 连通块中点的数量 题解 #include <iostream> #include <cstdio> using namespace std; const int N = 1e5 + 10; int p[N], cnt[N], n, m, x, y; int find(int x 阅读全文
posted @ 2022-05-18 21:29 czyaaa 阅读(31) 评论(0) 推荐(1) 编辑
摘要:并查集 处理问题 1.将两个集合合并 2.询问两个集合是否在一个集合当中 暴力思维 1.询问两个元素是否在一个集合 belong[x] = a; //元素x在a集合 if(belong[x] == belong[y]) //判断a与b是否在一个集合,复杂度O(1) 2.合并集合 //将a,b集合合并 阅读全文
posted @ 2022-05-18 20:46 czyaaa 阅读(75) 评论(0) 推荐(0) 编辑
摘要:AcWing835. Trie字符串统计 题解 Trie高效存储和查询字符串集合的数据结构 Trie树存储:结尾打上标记,表示为结尾 Trie树查找:根据逐个字符查找,能查找到所有字符且具有结尾标记。 #include <iostream> using namespace std; const in 阅读全文
posted @ 2022-05-16 19:35 czyaaa 阅读(32) 评论(0) 推荐(0) 编辑
摘要:ACWing3302.表达式求值 题解 #include <iostream> #include <stack> #include <unordered_map> #include <cstring> using namespace std; unordered_map<char, int> pr{ 阅读全文
posted @ 2022-05-16 16:57 czyaaa 阅读(33) 评论(0) 推荐(0) 编辑
摘要:讲解 我的KMP博客 AcWing831.KMP字符串 题解 本题用find函数会超时,因为find并没有对于找到子串的情况下次使用next。 KMP改进模式 下标从1开始 #include <iostream> #include <cstdio> using namespace std; cons 阅读全文
posted @ 2022-05-14 10:14 czyaaa 阅读(34) 评论(0) 推荐(0) 编辑
摘要:AcWing154. 滑动窗口 题解 暴力做法:直接遍历窗口里的所有元素找出最小值或最大值 寻找性质: 寻找最小:当 a[i] >= a[j] 且 i < j 时, 若在一个窗口中,则可以不考虑a[i],故每次找最小值找队头a[hh]即可 例子: 故整个队列是严格单调的,故称为单调队列 #inclu 阅读全文
posted @ 2022-05-12 20:44 czyaaa 阅读(69) 评论(0) 推荐(0) 编辑
摘要:AcWing830.单调栈 题解 暴力思维:把左边的数放在一个栈,每次从栈中找左边第一个小的数 寻找性质: 什么数该入栈,什么数该出栈? 当 a[i] >= a[j](注意这里一定要等于可以节省很多入栈操作) 且 i < j 时, a[i]永远都不会用到,所以我们应该出栈a[i],入栈a[j]. 由 阅读全文
posted @ 2022-05-12 19:52 czyaaa 阅读(31) 评论(0) 推荐(0) 编辑
摘要:AcWing829.模拟队列 #include <iostream> using namespace std; const int N = 1e5+10; int q[N], hh, tt; int main() { int n, k; cin >> n; string c; while( n -- 阅读全文
posted @ 2022-05-11 19:23 czyaaa 阅读(25) 评论(0) 推荐(0) 编辑
摘要:AcWing828.模拟栈 #include <iostream> using namespace std; const int N = 1e5+10; int stk[N], tt; int main() { int n, k; cin >> n; string c; while(n -- ) { 阅读全文
posted @ 2022-05-11 19:22 czyaaa 阅读(33) 评论(0) 推荐(0) 编辑
摘要:使用数组原因 因为new/malloc一个空间是非常慢,用数组模拟链表效率更高。 单链表:邻接表——AcWing826.单链表 题目 #include <iostream> #include <cstdio> using namespace std; const int N = 1e5+10; in 阅读全文
posted @ 2022-05-11 19:02 czyaaa 阅读(69) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示