经典算法
文章平均质量分 54
Andy Dennis
知足而上进,温柔且坚定,用代码书写身边的世界。
展开
-
算法小结例题剖析系列
前言之前为了打蓝桥和天梯,写了一些专题类的文章,这里将他们合一起,方便索引和总结。算法划分二叉树二叉树例题剖析三种遍历要会,然后学会用递归解决树的一系列问题。动态规划动态规划专题并查集并查集例题剖析搜索(+回溯法)搜索(+回溯法)例题剖析(一)搜索(+回溯法)例题剖析(二)具体划分排列组合排列组合实现字符串字符串操作例题剖析STLSTL常用操作利用unordered_map快速搭建数据结构...原创 2021-06-02 16:50:07 · 339 阅读 · 0 评论 -
T1292 宠物小精灵_二维背包_动态规划
前言师姐问了我一个二维背包问题, 傻眼了,之前没写过, 好在参考了老哥代码 T1292:宠物小精灵之收服 后给写出来了。作者使用c, 我使用cpp, 并且使用了STL, 更加省空间。题目大家看那个作者的链接吧。代码struct 不熟悉的同学可看: cpp极简入门——结构体_(7)STL 不熟悉的同学可看: STL常用操作#include <iostream>#include <unordered_map>using namespace std;struct Pe原创 2022-03-26 00:31:14 · 1013 阅读 · 0 评论 -
bfs搜索(+回溯法)例题剖析(一)
前言dfs搜索(+回溯法)例题剖析(一)dfs搜索(+回溯法)例题剖析(二)bfs搜索(+回溯法)例题剖析(一)原创 2021-06-04 10:38:12 · 625 阅读 · 0 评论 -
贪心例题剖析
前言题目答疑(20分)输入样例:310000 10000 1000020000 50000 2000030000 20000 30000输出样例:280000思路: 贪心策略, 不过输出答案的时候注意,是没有包括离开所需要的时间e的。排序的时候呢,肯定是 s + a + e小的排前面,如果一样则 s + a小的排前面。#include <iostream>#include <algorithm>using namespace std;str原创 2021-06-03 16:08:17 · 102 阅读 · 0 评论 -
STL常用操作
前言算法竞赛STL可以提高您的编程效率(c++的话)字符串find操作原创 2021-04-03 17:28:39 · 645 阅读 · 2 评论 -
二叉树例题剖析
三种遍历递归实现前序遍历为了大家方便看,我先把代码贴上,再贴上树的形状图。#include <iostream>using namespace std;int a[] = {1, 2, 3, 4, 5, 6};void preVis(int cur, int n) { if (cur >= n) return; cout << cur << " "; preVis(2 * cur + 1, n); preVis(2 * cur + 2, n原创 2021-04-14 11:33:51 · 778 阅读 · 0 评论 -
动态规划专题
前言积累一下动态规划专题。状态转移方程很重要,所以这一篇文章请重点留意状态转移方程。题目完全平方数(leetcode 279)该题来源于: https://leetcode-cn.com/problems/perfect-squares/给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。给你一个整数 n ,返回和为 n 的完全平方数的 最少数量 。完全平方数 是一个整数,其值等于另一个整数的平方;换句话说,其原创 2021-05-30 09:22:21 · 161 阅读 · 0 评论 -
并查集例题剖析
前言并查集经常用来判断群落等,或者是否连通。模板int parent[1050]int find(int x) { if (x != parent[x]) parent[x] = find(parent[x]); return parent[x];}void tryUnion(int a, int b) { int pa = find(a); int pb = find(b); if (pa != pb) parent[pa] = pb;}例题L3-003 社交原创 2021-04-22 21:05:15 · 152 阅读 · 0 评论 -
日期类例题剖析
前言关于日期的话,只要知道某个年是否是闰年,某个月的月份用列表存起来即可。例题剖析回文日期2020年蓝桥杯A组【样例输入】20200202【样例输出】2021120221211212#include <iostream>#include <algorithm>using namespace std;int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};bool isLe原创 2021-04-14 20:23:23 · 184 阅读 · 0 评论 -
dfs搜索(+回溯法)例题剖析(二)
前言补一补搜索…小字辈输入:92 6 5 5 -1 5 6 4 741 9#include <iostream>#include <vector>using namespace std;const int N = 1e5 + 10;vector<int> childLt[N];int depth[N], maxDepth;void dfs(int cur, int d) { depth[cur] = d; maxDepth =原创 2021-04-14 15:37:25 · 175 阅读 · 0 评论 -
dfs搜索(+回溯法)例题剖析(一)
D.迷宫【问题描述】下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。010000000100001001110000迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。 对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式, 其使用的步数最原创 2021-04-13 23:50:33 · 239 阅读 · 0 评论 -
字符串操作例题剖析
前言为了准备比赛,据说天梯赛字符串操作挺多的,所以也得找点例题来看看。例题剖析估值一亿的AI核心代码6Hello ? Good to chat with youcan you speak Chinese?Really?Could you show me 5What Is this prime? I,don 't knowHello ?AI: hello! Good to chat with youAI: good to chat with youcan y原创 2021-04-14 15:53:35 · 239 阅读 · 0 评论 -
素数(常规, 埃氏筛法)
前言刷蓝桥杯 2019国赛c++ B组(部分) 试题B 质数拆分的时候,遇到了求质数的另一种做法,这里记录一下常规埃氏筛法原创 2021-05-29 00:05:19 · 190 阅读 · 1 评论 -
algorithm next_permutation用法
前言刷蓝桥杯2019国赛c++ B组(部分), 试题G 排列数用到了algorithm 文件中的 next_permutation 函数.参考https://blog.csdn.net/tlonline/article/details/47297745, 这里列举一些例子比赛的时候直接调用爽啊。当然大佬们还是想自己写的可以看: 例子数组排序#include <iostream>#include <algorithm>#include <iomanip>原创 2021-05-28 23:21:27 · 208 阅读 · 0 评论 -
排列组合实现
前言本菜鸡今年第一次参加蓝桥杯,希望能捞个奖题目全排列参考: https://zhuanlan.zhihu.com/p/115504151#include <iostream>#include <algorithm>using namespace std;int a[] = {1, 2, 3};int cnt = 0;void perm(int start, int end) { // 找到一个全排列 if (start == end) { cout原创 2021-04-10 10:53:59 · 210 阅读 · 0 评论 -
利用unordered_map快速搭建数据结构
链表1. 简单示例#include <unordered_map>#include <vector>#include <iomanip>#include <iostream> using namespace std;struct LNode { int addr; int value; int next;};int main() { unordered_map<int, LNode> node; cout <&原创 2021-04-22 21:01:58 · 222 阅读 · 0 评论 -
freopen C重定向
头文件#include <cstdio>示例#include <iostream>#include <cstdio>#include <cmath>using namespace std;int main() { freopen("a.txt", "r", stdin); int a, b; cin >> a >> b; cout << a << " " << b<原创 2021-04-14 20:01:17 · 96 阅读 · 0 评论 -
upper_bound lower_bound
前言折半搜索有时会用到。参考: 关于lower_bound( )和upper_bound( )的常见用法从小到大的数组#include <algorithm>#include <iostream>using namespace std;static bool cmp(int a, int b) { return a > b;}int main() { int a[6] = {1,2,4,7,15,34}; sort(a, a + 6); for原创 2021-04-13 16:07:42 · 95 阅读 · 0 评论 -
priority_queue STL
前言有用的库当然得熟悉熟悉。比赛才能加快编码效率。参考文章: 【c++】STL里的priority_queue用法总结priority_queue引入头文件#include <queue>push & top & pop压入用 push, 取队列头(堆顶) 用 top, 弹出队列头(堆顶) 用 pop 。可以看到默认大根堆。大根堆#include <iostream>#include <queue> using namespa原创 2021-04-13 15:28:31 · 129 阅读 · 0 评论 -
牛客网刷题
字符串排序这道题主要是因为想记录下stringstream的用法:来源:牛客网输入描述:多个测试用例,每个测试用例一行。每行通过,隔开,有n个字符,n<100输出描述:对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格示例1输入a,c,bbf,ddddnowcoder输出a,bb,cdddd,fnowcoder#include <iostream>#include <algorithm>#include <vector>原创 2021-03-21 15:07:19 · 587 阅读 · 0 评论 -
最小堆c++实现
前言恰逢春招面试时, 大厂们都会考察对基本数据结构的掌握情况。这里有一个重点就是利用数组实现堆。据说有些面试官会要求白板手写堆。虽然我们在编码过程中可以借助STL的优先队列来实现堆,但是掌握其原理还是很重要滴。堆的实现是时候搬出当年写数据结构的代码了。这里给了一4个最小堆的例子,而最大堆的实现与此例子差不多,故此处就不赘述了。最小堆的定义//// Created by Dell on 2019/12/15.//#include <iomanip>#include <i原创 2021-03-06 15:42:55 · 858 阅读 · 2 评论 -
0-1背包空间层面优化
原始版本int knapsack(const vector<int> &w, const vector<int> &v, int C) { assert(w.size() == v.size() && C >= 0); int n = w.size(); if(n == 0|| C == 0) return 0; vector<vector<int>> memo(n, vector<int>(C+原创 2021-03-05 23:38:48 · 72 阅读 · 0 评论 -
2021winter leetcode刷题(coding)
前言开始抱一下佛脚。原创 2021-02-21 23:41:45 · 565 阅读 · 0 评论