![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构与算法
Nastu_Ho-小何同学
这个作者很懒,什么都没留下…
展开
-
求二叉树的宽度与二叉树的层数
用层序遍历来实现,两个问题使用的方法相近;void BinaryTreeWidth_level(TreeNode* root, int &width, int &height){ if(root == NULL) { height = 0; width = 0; return ; } TreeNode* p = root; queue<TreeNode*> Q;原创 2021-07-16 23:31:08 · 68 阅读 · 1 评论 -
输出二叉树某个结点值为X的结点所有公共结点
解决思路打印公共结点,涉及自底向上查找,后序遍历;怎么判定祖先结点呢?从简单的起;如果该点的左孩子为x或右孩子为x,则该点为父节点,也是祖先结点之一;往上一层;如果该点的左子树有x 或右子树有x,则该点为祖先结点之一递归参数:要判断是否找到x和找到祖先结点,返回值取bool递归边界:root为空;找到x找到祖先结点;递归逻辑:findAllAncestor(root->lc, x) || findAllAncestor(root->lc, x)bool findAl原创 2021-07-16 00:24:31 · 280 阅读 · 0 评论 -
问题 B: 树的高度
问题链接解题思路要求树的高度,可从起子树中的最大高度 + 1 解得代码#include<stdio.h>#include<algorithm>#include<vector>using namespace std;vector<int> child[1010];int n;int DFS(int root){ if(n == 1) return 1; if(child[root].size() == 0原创 2021-02-25 16:47:33 · 163 阅读 · 0 评论 -
题目编号 : B
问题链接解决思路根据完全二叉树的性质, 点m的左右孩子分别为 m*2 , m * 2 + 1(如果存在)满二叉树的结点数可利用公式pow(2, high) - 1;代码#include<stdio.h>#include<math.h>int m, n;int countNodes(int root){ int L = root, R = root; int hl = 0, hr = 0; while (L <= n) {原创 2021-02-24 21:26:17 · 78 阅读 · 0 评论 -
问题 E: 【宽搜入门】巧妙取量
题目链接解决思路设三个容器分别为 V1, V2, V3, 每次迭代都有6次操作(V1->V2;V1->V3;V2->V3;V2->V1;V3->V1;V3->V2)怎么判断要么倒空,要么装满?那V1->V2 即V1往V2里倒为例子V1不为空, 而V2未满, 假设V1当前容量为M,V2当前容量为N,V2中容量为S即要装满V2, 要倒入S - N; 要倒空V1,这要倒出M;故倒出量为min(M, S - N)代码#include<stdio.h&原创 2021-02-23 21:59:03 · 62 阅读 · 0 评论 -
问题 C: 【宽搜入门】8数码难题
题目链接解决思路struct Node{int x, y;int step;int last_pos[2]; // 0-x,1-y 记录空格位置,以防重走int M[3][3];};写入-入队-loop(出队-判断(越界,重走)-更新-判等-入队)代码#include<iostream>#include<queue>#include<algorithm>using namespace std;struct Node{ int原创 2021-02-23 17:02:27 · 67 阅读 · 1 评论 -
1103 Integer Factorization (30 分)
问题链接解决思路死胡同: sum == n;nowk == k; sum > n; nowk > k;岔路: 选或者不选优先从大的x^p 选入可保证有多种方案时保证得出的字典序大的方案代码#include<stdio.h>#include<vector>using namespace std;int n, k ,p, maxFacSum = -1; // n-target; k-num; p- x^pvector<int> fac;原创 2021-02-23 14:29:56 · 62 阅读 · 0 评论 -
问题 B: 【递归入门】组合的输出
问题链接解决思路思路1:死胡同:已选的个数到达上限; 数的范围超出上限岔路:选择与不选择思路2:这个思路不太好想,想按全排列改动,但实在想不出怎么保证 递增,这是网上借鉴,链接找不到了关键在这一步for (int i = p[index - 1]; i <= n; i++) ,对for循环的改动代码思路1:#include<stdio.h>int n,k,p[22];void DSF(int index, int nowK){ if(nowK == k原创 2021-02-22 21:29:46 · 73 阅读 · 0 评论 -
问题 A: 【递归入门】全排列
问题链接解决思路死胡同:index == n + 1岔路:未纳入排列的点; 纳入排列-上锁-DFS-解锁代码#include<stdio.h>const int maxn = 11;bool isExist[maxn] = {false};int p[maxn], n;void DFS(int index){ if(index == n + 1) { for (int i = 1; i <= n; i++) {原创 2021-02-22 21:17:41 · 60 阅读 · 0 评论 -
问题 F: 【递归入门】走迷宫
题目链接解决思路岔路:上下左右死胡同:到达终点为了不走回头路或没有重复点,对经过的点 上锁-DFS-解锁代码#include<stdio.h>#include<iostream>using namespace std;const int maxN = 15 * 15 +10;const int maxn = 20;struct node{ int x, y;}Node[maxN];int n, m;int a[maxn][maxn];i原创 2021-02-22 21:11:31 · 117 阅读 · 1 评论 -
问题 E: 【递归入门】出栈序列统计
问题链接解决思路模拟进出栈;岔路:要么入栈,要么出栈;死胡同:入栈次数=n;出栈次数=n;出栈次数大于入栈次数代码#include<stdio.h>int n;int count = 0;void DFS(int push, int pop){ if(push == n && pop == n) { count++; return; } if(push > n || pop > n |原创 2021-02-22 20:26:02 · 64 阅读 · 0 评论 -
矩形嵌套
问题简述有n个矩形,每个矩形可以用a,b来描述,表示长和宽。矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度)。例如(1,5)可以嵌套在(6,2)内,但不能嵌套在(3,4)中。你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每一个矩形都可以嵌套在下一个矩形内。问题链接解决思路将每个矩形当成一个顶点,若可以嵌套,则两个顶点有边。记录最大矩形(顶点)的下标,转化为DGA最长路线问题代码#include<std原创 2021-02-16 13:36:10 · 212 阅读 · 0 评论 -
1040 Longest Symmetric String (25 分)
问题简述给一字符串,判断当中最长的回文子串,输出其长度解决思路动态规划解决用一个dp[i][j] 来表示 str[i] 到 str[j] 中最长回文子串的长度显然 dp[i][i] 应初始化为 1;如果str[i] == str[j], 且 dp[i+1][j-1] == 1,即长度减1的子串也是回文串则dp[i][j] 就是回文串。不满足上两个条件则定不是回文串代码#include<stdio.h>#include<string.h>using names原创 2021-02-15 21:04:24 · 46 阅读 · 0 评论 -
1045 Favorite Color Stripe (30 分)
问题简述一个人喜欢用特定的颜色来编织彩带,且用色要一定顺序(不要求所有喜欢的颜色都用上)。如今给出颜色序列,求满足要求的最长子序列,并给出长度。问题链接解决思路可以套用最长不下降子序列的模板。给特定颜色序列按递增编号,同时排除掉不喜欢的颜色dp[i] = max(1, dp[j] + 1) (j <= i; A[j] <= A[i])代码#include<stdio.h>#include<string.h>#include<algorit原创 2021-02-15 06:46:05 · 230 阅读 · 1 评论 -
1007 Maximum Subsequence Sum (25 分)
问题简述给出一个序列,求出最大子序列和,且给出该子序列的起始与终点处解决思路暴力解决,枚举每对起始与终点;但会超时;动态规划解决,只枚举右端点,另dp[i] 表示 以i结尾的连续序列的最大和, pre[i] 表示以i结尾的连续序列的最大和的起始处。代码#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 10010;int A[maxn];int dp[maxn] = {原创 2021-02-13 16:08:00 · 90 阅读 · 0 评论 -
最长不下降子序列(LIS)
最长不下降子序列(LIS)题目解题思路代码题目在一个数字序列中,找对最长一个的子序列(可以不连续),使得这个子序列是不下降(非递减)的。解题思路与上一篇最大连续子序列和类似,我们将dp[i] 设计为 以下标 i 为结尾的子序列的最大长度; 但与上一篇不同的是,上一篇是连续的,故仅考虑邻接前后关系,而这题是可不连续的,故不难想到要用到两重循环。A[j] > A[i] 则, dp[i] = 1; (j < i)A[j] ≤ A[i], 且 d[j] + 1 > d[i],原创 2021-02-08 22:06:46 · 202 阅读 · 0 评论 -
最大连续子序列和
单纯记录一下设计状态:让dp[i] 为 必须以 下标 i 结尾的连续序列的最大和。则要求的最大子序列和就在d[0] 与 d[n-1] 中。dp[0] = A[0]dp[i] = A[i]dp[i] = dp[i - 1] + A[i]其中 1, 2 可以合并;故 dp[i] = max(A[i], dp[i - 1] + A[i])代码#include<stdio.h>#include<algorithm>using namespace std;const原创 2021-02-08 17:49:24 · 64 阅读 · 0 评论 -
数塔问题
数塔问题是动态规划一个很基本的练手题,来整一整吧;问最后路径上所有数字最大和为多少?自顶向下递归写法——假设顶点权值大于0; 共n层,编号为1- n#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 1000;int f[maxn][maxn], dp[maxn][maxn] = {0};int n;int DP(int i, int j){ if(dp[i]原创 2021-02-08 15:29:56 · 315 阅读 · 0 评论 -
1013 Battle Over Cities (25 分)
1013 Battle Over Cities 解决思路代码题目解决思路如果删除某点后导致图不连通, 图中剩下N个连通块,这需要补 N - 1 条路如果删除某点后图仍连通, 则无需补;并查集思路,删除某点后,看还剩多少个集合代码遍历图,当遇到要删除的点,则停止往下遍历邻接表#include<stdio.h>#include<vector>#include<string.h>using namespace std;const int maxn =原创 2021-02-05 21:43:05 · 47 阅读 · 0 评论 -
最小生成树-kruskal
每次加入边权最小且两点不连通的边如何判断两个端点是否在不同联通块?并查集如何跳出循环?n个节点的最小生成树有n-1条边#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, edg原创 2021-02-05 11:03:29 · 51 阅读 · 0 评论 -
最小生成树-prim
代码与Dijkstra相似#include<stdio.h>#include<vector>#include<algorithm>using namespace std;const int maxv =510;const int INF = 1000000000;int n, G[maxv][maxv];int d[maxv];bool vis[maxv] = {false};struct Node{ int v; int dis原创 2021-02-05 10:59:08 · 42 阅读 · 0 评论 -
拓扑排序
每次将入度为0的点加入队列。取出队头,将该顶点指向的顶点入度–循环直至队列为空#include<stdio.h>#include<vector>#include<queue>using namespace std;const int maxv = 110;vector<int> Adj[maxv];int n, m, inDegree[maxv];bool topologicalSort(){ int num = 0;原创 2021-02-05 10:57:14 · 59 阅读 · 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++) { for (int i = 0; i < n; i++)原创 2021-02-04 23:01:17 · 46 阅读 · 0 评论 -
SPEF
单纯记录一下#include<stdio.h>#include<vector>#include<queue>using namespace std;const int maxv = 510;const int INF = 100000000;struct Node{ int v; int dis; Node(int _v, int _dis):v(_v), dis(_dis){}};vector<Node> Adj[原创 2021-02-04 22:51:07 · 177 阅读 · 0 评论 -
1030 Travel Plan (30 分)
1030 Travel Plan 问题简述解决思路代码问题简述N个城市有M条路,并且给出M条路的Distance 和 cost。求起点到终点的最短路径、最短距离及花费,若有多条则选择花费最小的路径(花费最小唯一)。解决思路思路 1:直接求出最短路径且花费最小的路径思路2:先把所有最短路径求出,再根根据花费最小来选出路径代码思路1#include<stdio.h>#include<algorithm>using namespace std;const int m原创 2021-02-04 19:29:50 · 98 阅读 · 0 评论 -
1003 Emergency (25 分)
1003 Emergency 问题简述解决思路代码问题简述N个城市之间有M条路径,要求起点到终点最短路径数量且最短路径上救援小组之和最大的路径解决思路单源最短路径问题,Dijskra在找最短路径同时找出救援小组之和最大的路径代码#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxv = 510;const int INF =原创 2021-02-04 18:21:54 · 77 阅读 · 0 评论 -
Dijkstra算法
主要思想在未被访问的数组中 找到可达距离最小的点,将其纳入访问集合基于第一步找到的点为中介点,更新可达点的最短距离代码#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>using namespace std;const int maxv = 1000;const int INF = 1000000000;int n, G[maxv][maxv]原创 2021-02-03 15:14:32 · 77 阅读 · 0 评论 -
图的遍历之——BFS
邻接矩阵#include<stdio.h>#include<queue>using namespace std;const int maxv = 1010;const int INF = 1e12;int n, G[maxv][maxv];bool inq[maxv] = {false};void BFS(int u){ queue<int> q; q.push(u); inq[u] = true; while(!q.原创 2021-02-02 13:44:01 · 50 阅读 · 0 评论 -
图的遍历之-DFS
一般表示图有两种方法,一是邻接矩阵,另一种是邻接表邻接矩阵#include<cstdio>const int maxv = 1000;const int INF = 1e10; // 很大的数,表示不可达int n, G[maxv][maxv];bool vis[maxv] = {false};#include<cstdio>const int maxv = 1000;const int INF = 1e10; // 很大的数,表示不可达原创 2021-02-01 15:56:43 · 78 阅读 · 0 评论 -
1098 Insertion or Heap Sort (25分)
1098 Insertion or Heap Sort 问题简述解决思路代码问题简述给出两串序列,问用的是哪一种排序算法(插入 或 堆排序),且输出该种算法的下一趟结果问题链接解决思路主要考的是插入排序算法的实现 与 建堆和堆排序的实现代码#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 111;int origin[maxn], tempOri1[maxn],te原创 2021-01-31 21:11:25 · 41 阅读 · 0 评论 -
1107 Social Clusters (30分)
1107 Social Clusters 问题简述解决思路代码问题简述每个人都有自己喜欢的活动,如果两人之间有共同喜欢的活动,则属于同一集合;给出每个人的喜欢的活动,要求输出可分多少个集合,每个集合中有多少人解决思路并查集,根据是否有共同爱好活动,决定是否合并为一个集合用一个father数组,保存每个人的父节点。当要合并时,则修改father数组用一个course数组,来存喜欢某个活动的人,辅助判断是否合并代码#include<stdio.h>#include<algo原创 2021-01-31 17:09:23 · 64 阅读 · 1 评论 -
1066 Root of AVL Tree (25分)
1066 Root of AVL Tree问题简述解决思路代码问题简述给出结点数 与 结点值,要求构建一棵AVL,输出根节点解决思路AVL的性质,平衡因子的绝对值 小于等于1; 不平衡时,左旋 与 右旋操作代码#include<stdio.h>const int maxn = 110;int sqe[maxn];int max(int a, int b){ return a > b? a : b;}struct node{ int data;原创 2021-01-31 15:09:48 · 72 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30分)
1099 Build A Binary Search Tree 问题简述解决思路代码问题简述给出结点数N,结点编号为 0 - N, 按序给出每个结点的左右孩子结点编号,当不存在时用 -1 表示,最后一行给出结点值。要求构建一棵BTS,且给出其层序遍历。–问题链接解决思路由于数据按编号给出,用静态数组构建树比较方便。BTS的中序遍历特点就是 其序列为递增序列,顾可利用这一点来构建树。最后层序遍历–广度优先,借助队列实现代码#include<stdio.h>#include<qu原创 2021-01-31 14:14:42 · 40 阅读 · 0 评论 -
1064 Complete Binary Search Tree (30分)
1064 Complete Binary Search Tree完全二叉搜索树 问题简述解决思路代码问题简述给出一段非负整数序列,构建一棵完全二叉搜索数,要求给出其层序遍历的序列解决思路完全二叉树的性质BTS的性质故根据完全二叉树的性质,用数组存储,可根据下标得到父子关系;再者利用BTS的性质,中序遍历为递增序列最后建成的完全二叉搜索树也用数组表示,那如何求其层序遍历呢?直接遍历数组即可代码#include<stdio.h>#include<algorithm&g原创 2021-01-30 22:34:16 · 95 阅读 · 0 评论 -
1043 Is It a Binary Search Tree (25分)
1043 Is It a Binary Search Tree 问题简述解决思路代码问题简述给出一棵BTS的创建序列,问该序列是否为BTS的先序序列 或 镜像BTS的先序序列如果是,则输出对应的后序遍历序列[问题链接](https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856)解决思路1、建树,按值 LC < root < RC 建树2、编写先序,后序 与 对应的镜像先序,后序3原创 2021-01-30 21:54:48 · 61 阅读 · 0 评论 -
1053 Path of Equal Weight (30分)
1053 Path of Equal Weight问题简述解题思路代码问题简述给结点数与非叶结点数 还有目标权值;接着再给出每个结点的权值;然后给出每个非叶结点的编号,孩子个数,孩子编号要求给出从根节点到叶子结点的带权路径和为 目标权值 的路径(输出权值);如果有多条,则按非递增顺序给出问题链接解题思路可用DFS;由于要求按非递增顺序给出,则在整理权值时先把每个结点孩子结点排序按权值非递增顺序排列好,后面如果得到要求的路径则直接输出即可。需要一个数组来记录符合要求的结点编号代码#incl原创 2021-01-30 17:22:02 · 43 阅读 · 0 评论 -
1004 Counting Leaves (30分)
@[TOC](1004 Counting Leaves (30分))问题简述给出总结点数 和 非叶结点数;接着给出每个非叶结点的编号,孩子个数,孩子编号要求给出树的每一次叶子结点个数;[问题链接](https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184)解决思路DFS与BFS都可解决代码DFS#include<stdio.h>#include<algorithm&g原创 2021-01-30 16:00:24 · 43 阅读 · 0 评论 -
1106 Lowest Price in Supply Chain (25分)
@[TOC](1106 Lowest Price in Supply Chain (25分))问题简述有一个总供应商以 P价格提供一商品,每级分销商升加%p 向下一级出售,最终面向客户的称为零售商求最终最低零售价是多少,且有多少个这样的零售商解决思路即求深度最小的叶子结点,及其个数;可用DFS解决代码C16 KB时间限制250 ms内存限制64 MBC (gcc 6.5.0)1#include<stdio.h>2#include<math.h>原创 2021-01-30 14:03:45 · 61 阅读 · 0 评论 -
1094 The Largest Generation (25分)
@[TOC](1094 The Largest Generation (25分))问题简述给出每个非叶结点的层号,编号, 孩子结点的数量,以及编号要求给出结点数量最多的层号,以及该层的结点数[问题链接](https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048)解决思路DFS或BFS都行,需要一个记录每层结点数的数组代码C#include<stdio.h>#define m原创 2021-01-30 13:25:04 · 35 阅读 · 0 评论 -
1079 Total Sales of Supply Chain (25分)
@[TOC](1079 Total Sales of Supply Chain (25分))问题简述题目给出一个根供应商以P价格出售某种商品;每层经销商都升价百分之R售出;最后经零售商面向客户出售商品问总共买出多少钱[问题链接](https://pintia.cn/problem-sets/994805342720868352/problems/994805388447170560)解题思路主要思路可以用DFS来解决;如何存储数据:如果是C++,可以用vector来存放每个结点的孩子,原创 2021-01-29 18:28:19 · 73 阅读 · 0 评论