PAT刷题笔记
李大黑太白
这个作者很懒,什么都没留下…
展开
-
1066 Root of AVL Tree(构建AVL树模板)
1066 Root of AVL Tree(构建AVL树模板)构建AVL树#include <iostream>#include <string>#include<algorithm>#include<stack>#include<string.h>using namespace std;typedef int ElementType;typedef struct TNode* Position;typedef Posit原创 2020-09-02 20:59:19 · 208 阅读 · 1 评论 -
1064 Complete Binary Search Tree(完全二叉树中序与层序遍历关系)
1064 Complete Binary Search Tree(完全二叉树中序与层序遍历关系)解法一:建树#include <iostream>#include <string>#include<algorithm>#include<queue>#include<string.h>using namespace std;typedef int ElementType;typedef struct TNode* Positio原创 2020-09-02 20:41:17 · 174 阅读 · 0 评论 -
1057 Stack(树状数组+二分)
1057 Stack(树状数组+二分)用树状数组求插入栈中的数据的中位数(第k小的数):将插入的数据作为树状数组的下标就可以自动完成排序,更新对应结点的数值,表示该结点覆盖的区间内的节点数,通过比较从开始到该节点区间内的数据个数和中位数的位置的大小关系可以找到中位数。#include <iostream>#include <string.h>#include<stack>#include<algorithm>using namespace s原创 2020-09-02 17:02:21 · 146 阅读 · 1 评论 -
1049 Counting Ones(数学题)
1049 Counting Ones(数学题)本题为数学问题,遍历数字的各个位置根据下列公式:将数字分为left、now、right三个位置(a表示now的位置)1.当now=0时,now位置出现1的次数就取决于left,ans+=lefta;2.当now=1时,now位置出现1的次数比情况1多0~right次,ans+=lefta+right+1;3.当now>1时,now位置出现1的次数相当于left+1的情况,因为1已经出现过了,ans+=(left+1)*a;#include &原创 2020-09-02 11:14:57 · 205 阅读 · 0 评论 -
1045 Favorite Color Stripe(最长不下降子序列)
1045 Favorite Color Stripe(最长不下降子序列)题意:按照题目给出的颜色序列找出原颜色序列中对应的子序列,给出的颜色序列不需要在子序列中全部出现。解法:根据给出的序列对相应的颜色赋权值,就可以将颜色序列变成递增的权值序列,同样将原序列中出现的对应颜色变为相应权值,然后再找最长不下降子序列。(动态规划)#include <iostream>#include<map>#include<vector>#include<algorith原创 2020-09-02 08:50:13 · 170 阅读 · 0 评论 -
1040 Longest Symmetric String(最长回文串)
1040 Longest Symmetric String本题可以使用暴力遍历的方法解决,记录动态规划解法:根据子串在原字符串中的首尾的不同位置,可以构建一个动态规划的数组dp[n][n],初始化对角线元素为1(即长度为1的子串均为回文串),初始化dp[i][i+1]元素(即长度为2的子串)遍历二维数组dp[n][n],根据原字符串i,j位置的字符是否相等以及dp[i+1][j-1]是否为1,完成dp数组的构建,记录i,j的最大距离(j=i+L-1)。#include<iostream>原创 2020-09-01 21:29:50 · 149 阅读 · 0 评论 -
1018 Public Bike Management(DFS+dijkstra,最短路径加附加条件判断模板)
1018 Public Bike Management(DFS+dijkstra,最短路径加附加条件判断模板)There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the原创 2020-09-01 16:47:22 · 138 阅读 · 0 评论 -
PAT菜鸡笔记1021 Deepest Root
1021 Deepest RootA graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.Input Spec原创 2020-09-01 14:57:07 · 98 阅读 · 0 评论 -
单源最短路径模板
dijkstra(最短路径模板)在未收录节点中找最小者,收录,找到该节点的最短路径while (true) { int v = -1, min = Max; for (int i = 0; i < N; i++) {//找未收录节点的最小者 if (!visited[i] && len[i] < min) { v = i; min = len[i]; } } if (v == -1)break; visited[v] = t原创 2020-08-31 20:56:24 · 114 阅读 · 0 评论 -
前序中序转后序,后序中序转前序,前序后序转中序
将前序序列存入pre数组,中序序列存入in数组,后序序列存入post数组,则:前序中序—>后序#include <cstdio>using namespace std;int pre[] = {1, 2, 3, 4, 5, 6};int in[] = {3, 2, 4, 1, 6, 5};void post(int root, int start, int end) { if(start > end) return ; int i = start;原创 2020-06-06 21:32:12 · 1150 阅读 · 0 评论 -
PAT菜鸡笔记1119 Pre- and Post-order Traversals
PAT菜鸡笔记1119 Pre- and Post-order Traversals#include <iostream>#include<vector>using namespace std;int pre[30], post[30];int n,flag=1;vector<int> in;void In(int postroot, int preroot,int end_onpre,int start_onpost){ if (preroot原创 2020-05-22 19:29:48 · 147 阅读 · 0 评论 -
PAT菜鸡笔记1007 Maximum Subsequence Sum
PAT菜鸡笔记1007 Maximum Subsequence Sum思路:这题就是找最大子列和和对应位置的元素,这个好像是有标准模板的,我的代码是根据那个思路自己写的,可能比较奇怪。先取序列第一项作为最大子列和,遍历序列。设当前遍历到第j个元素,则会出现以下几种情况:sum<=0且第j个元素>=0,则放弃之前的子列,将左部、sum均归到这个位置,更新maxsum>0...原创 2020-03-03 18:45:24 · 192 阅读 · 0 评论 -
PAT菜鸡笔记1006 Sign In and Sign Out
PAT菜鸡笔记1006 Sign In and Sign Out思路:本题我使用了vector存结构体信息,写两个cmp对进入时间和出去时间分别排序,再输出就行了#include<iostream>#include<algorithm>#include<vector>#include<string.h>using namespace s...原创 2020-03-03 11:01:04 · 89 阅读 · 0 评论 -
PAT菜鸡笔记1005 Spell It Right
PAT菜鸡笔记1005 Spell It RightGiven a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.Input Specification:Each input file co...原创 2020-02-20 12:48:26 · 67 阅读 · 0 评论 -
PAT菜鸡笔记1004 Counting Leaves
PAT菜鸡笔记1004 Counting LeavesA family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one t...原创 2020-02-20 12:38:26 · 189 阅读 · 0 评论 -
PAT菜鸡笔记1003 Emergency
PAT1003 EmergencyAs an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in e...原创 2020-02-19 22:51:00 · 105 阅读 · 0 评论 -
PAT菜鸡笔记1002 A+B for Polynomials
PAT1002 A+B for Polynomials注意点:无,方法无脑,加就完事了在这里插入代码片原创 2020-02-19 12:05:37 · 98 阅读 · 0 评论 -
PAT菜鸡笔记1001 A+B Format
PAT1001 A+B FormatCalculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).Input Specificat...原创 2020-02-19 11:00:40 · 174 阅读 · 0 评论