![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
《算法笔记》学习记录
balala
Matts Tian
这个作者很懒,什么都没留下…
展开
-
DP——完全背包问题
#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 100;const int maxv = 1000;int w[maxn], c[maxn], dp[maxn];int main(){ int n, V; scanf("%d%d", &n, &V); for(int i = 0; i < n; i++) { scanf("%d", &a.原创 2021-03-11 21:25:58 · 183 阅读 · 0 评论 -
DP——01背包问题
#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 100;const int maxv = 1000;int w[maxn], c[maxn], dp[maxn];int main(){ int n, V; scanf("%d%d", &n, &V); for(int i = 0; i < n; i++) { scanf("%d", &a.原创 2021-03-11 21:13:51 · 111 阅读 · 0 评论 -
DP——最长回文子串
#include<stdio.h>#include<string.h>const int maxn = 1010;char S[maxn];int dp[maxn][maxn];int main(){ gets(S); int len = strlen(S); int ans = 1; memset(dp, 0, sizeof(dp)); for(int i = 0; i < len; i++) { dp[i][i] = 1; if(i &l.原创 2021-03-11 20:10:53 · 109 阅读 · 0 评论 -
DP——最长公共子序列(LCS)
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 100;char A[maxn], B[maxn];int dp[maxn][maxn];int main(){ int n; gets(A + 1); gets(B + 1); int lenA = strlen(A + 1); int lenB = strl..原创 2021-03-11 16:31:02 · 101 阅读 · 0 评论 -
DP——最长不下降子序列(LIS)
#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 100;int A[maxn], dp[maxn];int n;int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &A[i]); } int ans = -1..原创 2021-03-11 15:47:36 · 203 阅读 · 0 评论 -
DP——最大连续子序列和
#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 10010;int A[maxn], dp[maxn];int main(){ int n; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &A[i]); } dp[0] = A[0]; for(int i = 1; .原创 2021-03-11 15:16:14 · 96 阅读 · 0 评论 -
DP——数塔问题
#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 1000;int f[maxn][maxn], dp[maxn][maxn];int main(){ int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { scanf(..原创 2021-03-11 15:08:16 · 69 阅读 · 0 评论 -
关键路径
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<stack>#include<vector>using namespace std;const int MAXV = 510;const int INF = 1000000000;int inDegree[MAXV], n, m;int ve[MAXV], vl原创 2021-03-10 10:13:06 · 48 阅读 · 0 评论 -
拓扑排序
vector<int> G[MAXV];int n, m, inDegree[MAXV];bool topologicalSort(){ int num = 0; queue<int> q; for(int i = 0; i < n; i++) { if(inDegree[i] == 0) { q.push(i); } } while(!q.empty()) { int u = q.front(); q.pop(); fo..原创 2021-03-09 21:44:40 · 64 阅读 · 0 评论 -
最小生成树——kruskal
#include<stdio.h>#include<algorithm>using namespace std;const int MAXV = 110;const int MAXE = 10010;struct edge{ int u, v; int cost;}E[MAXE];bool cmp(edge a, edge b){ return a.cost < b.cost;}int father[MAXV];int findFathe...原创 2021-03-09 21:03:12 · 62 阅读 · 0 评论 -
最小生成树——prim
#include<stdio.h>#include<algorithm>using namespace std;const int MAXV = 1000;const int INF = 1000000000;int n, m, G[MAXV][MAXV];int d[MAXV];bool vis[MAXV] = {false};int prim(){ fill(d, d + MAXV, INF); d[0] = 0; int ans = 0; fo..原创 2021-03-09 20:36:34 · 130 阅读 · 0 评论 -
最短路径——Floyd
#include<stdio.h>#include<algorithm>using namespace std;const int INF = 1000000000;const int MAXV = 200;int n, m;int dis[MAXV][MAXV];void Floyd(){ for(int k = 0; k < n; k++) { // n 次循环 for(int i = 0; i < n; i++) { // 更.原创 2021-03-09 20:10:12 · 59 阅读 · 0 评论 -
Dijkstra——Travel Plan
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>using namespace std;const int MAXV = 1010;const int INF = 1000000000;int n, m, st, ed, G[MAXV][MAXV], cost[MAXV][MAXV];int d[M..原创 2021-03-07 19:17:05 · 83 阅读 · 0 评论 -
Dijkstra——Emergency
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int MAXV = 1010;const int INF = 1000000000;int n, m, st, ed, G[MAXV][MAXV], weight[MAXV];int d[MAXV], w[MAXV], num[MAXV];bool vis[MAXV] = {false};...原创 2021-03-06 21:26:47 · 62 阅读 · 0 评论 -
Dijkstra 的三种特殊情况
① 给每条边再增加一个边权(比如花费),然后要求在最短路径有多条时要求路径上的花费之和最小(如果边权是其他含义,也可以是最大)。void Dijkstra(int s){ fill(d, d + MAXV, INF); d[s] = 0; for(int i = 0; i < n; i++) { // 循环 n 次 int u = -1, MIN = INF; for(int j = 0; j < n; j++) { if(vis[j] == false &am原创 2021-03-06 20:52:39 · 182 阅读 · 0 评论 -
最短路径——Dijkstra
① 邻接矩阵int n, G[MAXV][MAXV];int d[MAXV];bool vis[MAXV] = {false};void Dijkstra(int s){ fill(d, d + MAXV, INF); d[s] = 0; for(int i = 0; i < n; i++) { // 循环 n 次 int u = -1, MIN = INF; for(int j = 0; j < n; j++) { if(vis[j] == false原创 2021-03-06 19:30:54 · 102 阅读 · 3 评论 -
图的 BFS 遍历
① 邻接矩阵#include<stdio.h>#include<queue>using namespace std;const int MAXN = 1000;const int INF = 1000000000;int n, G[MAXN][MAXN];bool inq[MAXN] = {false};void BFS(int u){ queue<int> q; q.push(u); inq[u] = true; while(!q.emp原创 2021-03-06 15:44:37 · 192 阅读 · 0 评论 -
图的 DFS 遍历
① 邻接矩阵#include<stdio.h>const int MAXV = 1000;const int INF = 1000000000;int n, G[MAXV][MAXV];bool vis[MAXV] = {false};void DFS(int u, int depth){ vis[u] = true; for(int v = 0; v < n; v++) { if(vis[v] == false && G[u][v] != INF原创 2021-03-05 20:30:15 · 145 阅读 · 0 评论 -
哈夫曼树
哈夫曼树是带权路径长度最小的数,又称为最优二叉树。#include<stdio.h>#include<queue>using namespace std;priority_queue< long long, vector<long long>, greater<long long> > q;int main(){ int n; long long temp, x, y, ans = 0; scanf("%d", &n原创 2021-03-05 14:49:53 · 53 阅读 · 0 评论 -
堆排序
#include<algorithm>using namespace std;const int maxn = 110;int heap[maxn], n = 10;void downAdjust(int low, int high){ int i = low, j = i * 2; while(j <= high) { if(j + 1 <= high && heap[j + 1] > heap[j]) j = j + 1; if(h原创 2021-03-04 21:36:19 · 64 阅读 · 1 评论 -
并查集——好朋友
#include<stdio.h>const int maxn = 110;int father[maxn];bool isRoot[maxn];void init(int n){ for(int i = 1; i <= n; i++) { father[i] = i; isRoot[i] = false; }}int findFather(int x){ while(x != father[x]) { x = father[x]; } retu.原创 2021-03-03 16:20:49 · 96 阅读 · 1 评论 -
并查集的基本操作
#include<stdio.h>#include<vector>#include<algorithm>using namespace std;const int maxn = 110;int father[maxn];void init(int N){ for(int i = 1; i <= N; i++) { father[i] = i; }}int findFather(int x){ while(x != father[.原创 2021-03-03 15:22:14 · 65 阅读 · 1 评论 -
AVL 的基本操作
LL 与 LRRR 与 RL插入及建立#include<stdio.h>#include<vector>#include<algorithm>using namespace std;struct node{ int v, height; node *lchild, *rchild;};node* newNode(int v){ node* Node = new node; Node->v = v; Node->he原创 2021-03-03 14:52:28 · 612 阅读 · 0 评论 -
BST—— Is it a Binary Search Tree
#include<stdio.h>#include<vector>using namespace std;struct node{ int data; node *left, *right;};void insert(node* &root, int data){ if(root == NULL) { root = new node; root->data = data; root->left = NULL; root-&g.原创 2021-03-02 11:09:52 · 83 阅读 · 0 评论 -
BST 的基本操作
插入及建立void insert(node* &root, int x){ if(root == NULL) { root = newNode(x); return; } if(x == root->data) return; else if(x < root->data) insert(root->lchild, x); else insert(root->rchild, x);}node* Create(int data[], in原创 2021-03-02 10:15:50 · 367 阅读 · 0 评论 -
树的DFS——Path of Equal Weight
#include<stdio.h>#include<vector>#include<algorithm>using namespace std;const int MAXN = 110;struct node{ int weight; vector<int> child;}Node[MAXN]; // 静态树bool cmp(int a, int b){ return Node[a].weight > Node[b]...原创 2021-03-02 09:26:12 · 112 阅读 · 0 评论 -
树的遍历
#include<stdio.h>#include<vector>#include<queue>using namespace std;const int maxn = 100;struct node{ int data; vector<int> child;}Node[maxn]; void PreOrder(int root){ printf("%d", Node[root].data); for(int i = 0; .原创 2021-02-24 09:28:22 · 58 阅读 · 0 评论 -
二叉树的遍历——还原二叉树
#include<stdio.h>#include<queue>using namespace std;const int maxn = 50;struct node{ int data; node* lchild; node* rchild;};int post[maxn], in[maxn];int n;node* create(int postL, int postR, int inL, int inR){ if(postL > postR.原创 2021-02-23 10:26:59 · 96 阅读 · 0 评论 -
二叉树的遍历——BFS
void LayerOrder(node* root){ queue<node*> Q; root->layer = 1; Q.push(root); while(!Q.empty()) { node* now = Q.front(); Q.pop(); printf("%d ", now->data); if(now->lchild != NULL) { now->lchild->layer = now->layer .原创 2021-02-23 09:02:10 · 86 阅读 · 0 评论 -
二叉树的遍历——DFS
先序遍历void preorder(node* root){ if(root == NULL) return; printf("%d\n", root->data); preorder(root->lchild); preorder(root->rchild);}中序遍历void inorder(node* root){ if(root == NULL) return; inorder(root->lchild); printf("%d\n", root.原创 2021-02-23 08:50:48 · 405 阅读 · 0 评论 -
BFS——走迷宫的最小步数
#include<stdio.h>#include<string.h>#include<queue>using namespace std;const int maxn = 100;struct node{ int x, y; int step;}S, T, Node;int n, m; char maze[maxn][maxn]; // 迷宫信息 bool inq[maxn][maxn] = {false};i..原创 2021-02-22 10:02:50 · 205 阅读 · 0 评论 -
BFS——求矩阵中“块”的个数
#include<stdio.h>#include<queue>using namespace std;const int maxn = 100;struct node{ int x, y;}Node;int n, m; int matrix[maxn][maxn]; // 01矩阵 bool inq[maxn][maxn] = {false};int X[4] = {0, 0, 1, -1};int Y[4] = {1, -1.原创 2021-02-22 08:36:24 · 321 阅读 · 0 评论 -
DFS——选数问题
#include<stdio.h>#include<vector> using namespace std;const int maxn = 30;int n, k, x, maxSumSqu = -1, A[maxn];vector<int> temp;vector<int> ans;void DFS(int index, int nowK, int sum, int sumSqu){ if(nowK == k && s.原创 2021-02-21 10:35:58 · 154 阅读 · 0 评论 -
DFS——背包问题
#include<stdio.h>const int maxn = 30;int n, V, maxValue = 0;int w[maxn], c[maxn];void DFS(int index, int sumW, int sumC){ // 死胡同 if(index == n) return; // 岔路口 —— 不选 DFS(index + 1, sumW, sumC); // 岔路口 —— 选 if(sumW + w[index] <= V).原创 2021-02-21 09:38:29 · 123 阅读 · 0 评论 -
静态链表——sorting
#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 100005;struct Node{ int address, data, next; bool flag;}node[maxn];bool cmp(Node a, Node b){ if(a.flag == false || b.flag == false) return a.flag > b.flag;.原创 2021-02-20 11:44:17 · 88 阅读 · 0 评论 -
静态链表——sharing
#include<stdio.h>const int maxn = 100010;struct NODE{ char data; int next; bool flag;}node[maxn]; int main(){ for(int i = 0; i < maxn; i++) { node[i].flag = false; } int s1, s2, n; scanf("%d%d%d", &s1, &s2, &n); int a.原创 2021-02-20 09:54:20 · 65 阅读 · 0 评论 -
栈与队列的应用——计算表达式的值
#include<iostream>#include<stdio.h>#include<string>#include<stack>#include<queue>#include<map>using namespace std;struct node{ double num; char op; bool flag;};string str;stack<node> s;queue<nod.原创 2021-02-19 12:05:07 · 165 阅读 · 0 评论 -
将中缀表达式转换为后缀表达式
我们把平时所用的标准四则运算表达式,即“9+(3-1)*3+10/2"叫做中缀表达式。因为所有的运算符号都在两数字的中间,现在我们的问题就是中缀到后缀的转化。 中缀表达式“9+(3-1)*3+10/2”转化为后缀表达式“9 3 1-3*+ 10 2/+” 规则:从左到右遍历中缀表达式的每个数字和符号,若是数字就输出,即成为后缀表达式的一部分;若是符号,则判断其与栈顶符号的优先级,是右括号或优先级低于找顶符号(乘除优先加减)则...转载 2021-02-19 09:48:34 · 756 阅读 · 0 评论 -
图解后缀表达式的计算过程
为了解释后缀表达式的好处,我们先来看看,计算机如何应用后缀表达式计算出最终的结果20的。 后缀表达式:9 3 1-3*+ 10 2/+ 规则:从左到右遍历表达式的每个数字和符号,遇到是数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,运算结果进栈,一直到最终获得结果。 下面是详细的步骤: 1. 初始化一个空栈。此桟用来对要运算的数字进出使用。 2. 后缀表达式中前三个都是数字,所以9、3、1进栈...转载 2021-02-19 09:18:50 · 2076 阅读 · 0 评论 -
algorithm 头文件下的常用函数
1. max()、min()、abs()2. swap()3. reverse(it, it2)4. next_permutation(a, a + 3)给出一个序列在全排列中的下一个序列。5. fill(a, a + 5, 233)6. sort(a, a + 3, cmp)7. lower_bound(first,last,val)用来寻找在数组或容器的 [ first, last ) 范围内第一个大于等于 val 的元素的位置。如果是数组,返回该位置的指针;如果是容器,返回该位置的迭代原创 2021-02-18 16:51:44 · 111 阅读 · 0 评论