自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(69)
  • 收藏
  • 关注

原创 leetcode 301. 删除无效的括号

建立决策树的BFS、DFS题。有两种解法。我的两种解法优化的都不是很好,不论是时间还是空间都没有进50%BFS解法要用set来解决掉重复元素。用unordered_set要比set快很多https://leetcode-cn.com/problems/remove-invalid-parentheses/class Solution {public: vector<string> res; void BFS(string s) { queue

2020-12-22 10:47:16 127

原创 近几年pat甲级真题考点总结

2020年秋 (82分)7-1 Panda and PP Milk (20分) 数学模拟7-2 How Many Ways to Buy a Piece of Land (25分) 动态规划。暴力模拟也能求出来,用sum数组保存前i个数的和,然后遍历即可7-3 Left-View of Binary Tree (25分) 树。树的遍历,利用树的遍历建树7-4 Professional Ability Test (30分) 图。DAG, Dijkstra, 拓扑排序建环。新题2020年春(83分)

2020-11-29 10:13:43 1189

原创 2019年冬季 7-4 Cartesian Tree (30分)

A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesia

2020-11-29 09:51:14 148

原创 7-1 Good in C (20分)

错误点:1.注意什么叫空行。输出一个换行符只能到下一行,再输出一个换行符才能是空行2.pta中,c++不能使用gets和gets_s函数(可能能用,但是没找到头文件)3.输入的字符串可能有空行,要用getline读入4.不要跟我一样手贱用scanf输入5.如果用getline(),警惕末尾分隔符的情况。如AS#include <iostream>#include <vector>#include <cstdio>#include <cstring&g

2020-11-28 21:26:10 159 1

原创 7-2 The Judger (25分)

集合题。删减了一些内容,每提交,可能通不过编译#include <iostream>#include <unordered_set>#include <vector>#include <algorithm>using namespace std;const int SIZE = 10005;int N, M;int adj[SIZE][SIZE];bool cmp(const int& a, const int& b)

2020-11-28 20:02:15 94

原创 codeup 关键路径 模板

#include <iostream>#include <vector>#include <queue>#include <stack>#include <algorithm>using namespace std;const int SIZE = 100;/*第一行输入一个正整数n(1<=n<=5),其代表测试数据数目,即图的数目第二行输入x(1<=x<=15)代表顶点个数,y(1<=y<=

2020-11-28 17:00:48 179

原创 pat甲级考试2020年春 7-4 Replacement Selection

https://pintia.cn/problem-sets/1287399212459409408/problems/1287399280939810818#include <iostream>#include <queue>#include <vector>using namespace std;const int SIZE = 1000005;int main() { int arr[SIZE]; priority_queue&.

2020-11-28 15:19:35 136

原创 2020年秋 7-1 Panda and PP Milk (20分)

从左往右判断。当轮到第i个熊猫时,为他分200牛奶,此时判断该熊猫满不满足。如果不满足,即i - 1的体重小于(或等于)它,就为它分mk[j - 1] + 100的牛奶(让它心情稳定下来)。如果该熊猫满足,判断左边的熊猫满不满足,调整到左边的熊猫满足之后,再判断左边的左边的熊猫满不满足……(用while循环就可以了)可以在草稿纸上跑一遍,能更顺畅的写出代码#include <iostream>using namespace std;const int SIZE = 10005;in

2020-11-28 09:35:24 489

原创 2020年秋季 7-4 Professional Ability Test (30分)

https://pintia.cn/problem-sets/1302814386427613184/problems/1302814455281307651有向无环图问题,可以多加一个点N让该图入度为0的点只有点N。用拓扑排序判环。注意D是边的属性,不能定义evD[SIZE],(evD[i]表示点的D,而D是边的属性而不是点的属性,这个定义明显是错的)#include <iostream>#include <vector>#include <algorithm&g.

2020-11-28 09:27:21 1614 2

原创 1030 Travel Plan (30分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392#include <iostream>#include <vector>#include <algorithm>using namespace std;const int SIZE = 1005;const int INF = 100000000;struct Node { int numb.

2020-11-24 19:44:00 113

原创 1003 Emergency (25分)

求最短路径的dij和bell方法。顺带求最短路径的条数和求多重标尺的做法#include <iostream>#include <vector>#include <algorithm>#include <set>using namespace std;const int S = 1000;const int INF = 100000000;struct Node { int number; int dis;

2020-11-24 19:41:25 94

原创 1133 Splitting A Linked List (25分)

链表排序题。用sort会出错,想让两个元素不进行比较维持原序列的顺序时,sort达不到目的。注意res2.size() == 0但是res3.size()不是0的情况。#include <iostream>#include <vector>#include <algorithm>using namespace std;const int SIZE = 10000005;struct Lnode { int address; int data

2020-11-24 09:06:58 77

原创 1134 Vertex Cover (25分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088图题。题目提出一个概念让你去推。明白概念即可。#include <iostream>#include <vector>#include <unordered_set>using namespace std;const int SIZE = 10005;vector<int> adj[SIZ.

2020-11-24 09:04:30 67

原创 1135 Is It A Red-Black Tree (30分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640本质上是DFS题注意红黑树是二叉搜索树#include <iostream>#include <algorithm>using namespace std;const int SIZE = 10005;struct Node { int data; int redOrBlack; //re.

2020-11-23 20:08:54 72

原创 1147 Heaps (30分)

#include <iostream>#include <algorithm>#include <vector>using namespace std;const int SIZE = 10010;const int INF = 99999999;int heap[SIZE];int M, N;bool cmp(const int& a, const int& b) { return a > b;}void post

2020-11-23 19:14:07 99

原创 1155 Heap Paths (30分)

#include <algorithm>#include <vector>#include <iostream>using namespace std;const int SIZE = 1005;int N;int heap[SIZE];bool visit[SIZE] = {false};vector<int> path;bool flagMAX = true;bool flagMIN = true;bool cmpMAX(co

2020-11-23 19:13:17 48

原创 1154 Vertex Coloring (25分)

#include <iostream>#include <vector>#include <unordered_set>using namespace std;const int SIZE = 10005;struct Lver { int color;} vers[SIZE];vector<int> adj[SIZE];int main() { int N, M, v1, v2; scanf("%d %d", &

2020-11-23 19:06:18 71

原创 1153 Decode Registration Card of PAT (25分)

错误点1:再cmpNt中用sscanf函数将string化为int,导致测试点4超时错误点2:将string用scanf输入后,如果不substr后比较||赋值,就会发生错误。可能和resize有关#include <iostream>#include <vector>#include <algorithm>#include <map>#include <unordered_map>using namespace std;const

2020-11-23 17:28:59 102

原创 1152 Google Recruitment (20分)

注意substr的用法,s.substr(i, j)的范围是[i , i + j)#include <iostream>#include <cmath>using namespace std;string s;bool isPrime(int num) { if (num <= 1) return false; int sqr = (int)sqrt((double)num); for (int i = 2; i <= sqr; i

2020-11-22 20:58:47 56

原创 1151 LCA in a Binary Tree (30分)

普通二叉树的LCA用二叉搜索树的方法会超时。因为不如二叉搜索树搜索的快。难点:递归,画图理解即可。#include <iostream>#include <algorithm>using namespace std;const int SIZE = 30005;int inOrder[SIZE];int preOrder[SIZE];struct Node { int data; Node* left,* right; Node() {l

2020-11-22 20:22:09 68

原创 1149 Dangerous Goods Packaging (25分)

#include <iostream>#include <unordered_map>#include <vector>#include <algorithm>using namespace std;const int SIZE = 1000000;vector<int> vt[SIZE];int order[SIZE];int main() { int N, M, first, second, K; cin &gt

2020-11-22 15:13:16 76

原创 1143 Lowest Common Ancestor (30分)

#include <iostream>#include <vector>#include <algorithm>using namespace std;const int SIZE = 80000;int postOrder[SIZE];int path[SIZE];int res;struct Node { int data; Node* left, * right; Node() { left = right = NULL; }

2020-11-22 15:07:41 102

原创 1146 Topological Order (25分)

#include <iostream>#include <vector>#include <algorithm>using namespace std;const int SIZE = 100005;vector<int> adj[SIZE];int degreeIn[SIZE];int temp[SIZE];int main() { int N, M; cin >> N >> M; int u, v; for

2020-11-22 15:06:15 41

原创 1150 Travelling Salesman Problem (25分)

#include <iostream>#include <vector>#include <algorithm>using namespace std;const int SIZE = 400;struct Node { int data; int dis; Node(int data, int dis) : data(data), dis(dis) {}} ;vector<Node> adj[SIZE];vector

2020-11-22 15:05:39 52

原创 1148 Werewolf - Simple Version (20分) 测试点3错误

列举撒谎者i,j,遍历数组得到狼。如果狼的size等于2,那么序列必须是(i,x)或(j,x) (x != i && x != j)如果狼的size等于1,那么该人x不能是i,j。因为如果x==i || x == j,另外一个狼就得不到。当x不是i,j时,有两种可能:(x, i) (x, j)列举即可。结果测试点3答案错误……求大佬解答#include <iostream>#include <set>#include <vector>#incl

2020-11-22 11:07:05 211

原创 pat 1034 Head of a Gang (30分)

/*没有按照顺序输出,导致测试点2、5错误*/#include <iostream>#include <vector>#include <map>#include <algorithm>using namespace std;const int M = 10005;int N, K;struct Node { string name; int weight = 0; int relationWeight = 0

2020-11-02 20:49:22 86

原创 pat 1086 Tree Traversals Again

思路 模拟遍历用自己的思路就会出错……折磨死了。自己还是太菜了。边遍历边建树/*思路:建树*/#include <iostream>#include <vector>#include <stack>#include <string>using namespace std;struct Node{ int data; Node *left; Node *right; Node() { left = right

2020-10-30 15:04:50 77

原创 求大神解答 pat甲级 1103 Integer Factorization (30分)

思路:回溯算法有点像N皇后建树:节点的两个属性:1.选择列表 2.路径选择列表就是n^2 <= N时,所有的n,假设有m个对array[1],总共有m个选择。选完之后,再对array[2]选……一直到array[K]。对array[n]的选择类似于对N皇后的第n行随机放皇后。还做过一个转盘锁的问题,用其思路,假设K = 5,初始是0 0 0 0 0,旋转之后又1 0 0 0 0, 0 1 0 0 0,0 0 1 0 0……共5中情况,然后继续旋转直到得到解。这个方法运行超时,不使用。#i

2020-10-29 16:10:24 155 1

原创 pat 贪心算法 1038 Recover the Smallest Number (30分) (未解决)

思路:贪心先取两个数a、b。要让两个数组合成的数最小,只需判断a-b(-表示连接)和b-a的大小即可。再从局部最优推广到全局最优,就可得到正确结果。测试点6:运行超时#include <iostream>#include <queue>#include <sstream>using namespace std;struct cmp { bool operator() (const string& a, const string& b

2020-10-27 21:01:02 129

原创 pat 贪心算法题 1067

1067 Sort with Swap(0, i) (25分)Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the s

2020-10-27 20:10:01 275

原创 pat B1020月饼

思路:贪心#include <iostream>#include <algorithm>#include <queue>#include <iomanip>using namespace std;const int N = 1010;double arrayK[N];double arrayP[N];struct cmp { template <typename T> bool operator() (cons

2020-10-26 19:08:58 105

原创 算法笔记第二章 个人遗漏知识点

整形数据前加unsigned,表示无符号型,即把负数范围挪到正数上来碰到浮点型数据用double而不是float小写字母的ASCII码比大写字母大32\0代表空字符NULL,\n代表换行double在scanf中用%lf,输出用%fscanf的%c可以读入空格跟换行输出% :printf("%%"); 输出\ : printf("\")用 typedef long long LL 提高编码效率常用math函数:fabs(double x) || ..

2020-10-25 18:24:46 52

原创 每日一题之拉低通过率 DFS/BFS leetcode 230 二叉搜索树

思路 DFS+优先队列BFS应该也可以/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr),

2020-10-23 10:18:15 61

原创 每日一题之拉低通过率 BFS/DFS leetcode 200 岛屿数量

思路1: 套用BFS模型从一个点开始,利用BFS遍历每一个小岛生成的群岛就是一个岛屿class Solution {private: int addrow[4] = {1, -1, 0, 0}; int addcol[4] = {0, 0, 1, -1};public: void BFS(int row, int col, vector<vector<bool>>& visited, vector<vector<char>&g

2020-10-21 16:44:13 83

原创 每日一题之被PTA折磨 7-38 表达式转换 (25分)

优先级比栈内的高,就放入;优先级比栈内的低,先把比自己低的弹出,再放入代码借鉴自https://blog.csdn.net/weixin_44996854/article/details/102752227,感谢大佬的思路#include <iostream>#include <stack>using namespace std;int lco(char c) { //放入栈之前 int num = -1; switch(c) {

2020-10-20 22:42:42 181

原创 每日一题之拉低通过率 BFS leetcode 127 单词接龙

思路 BFS套用BFS模型。用map保存每一层离树顶的长度即可。class Solution {public: int BFS(string beginWord, string endWord, vector<string>& wordList) { queue<string> qu; set<string> visit; qu.push(beginWord); visit.insert

2020-10-19 18:31:05 75

原创 每日一题之被疯狂折磨 动态规划 leetcode 5545()

思路 动态规划一开始用刚学会的回溯算法做,运行超时。改用动态规划,听了yxc的讲解,目前一知半解,好像省了很多步骤。第一次做这种题,先记住模型好思路,以后再战。题目最后得到的结果是一个类似于一个单调上升函数(是一连串的点,应该不算函数),x轴是年龄,y轴是分数。设分数从小到大,年龄也是从小到大,构成的点是对应x、y轴的球员。现在先把y轴的分数排列好,这时x轴是无序的。而最终结果一定是上升序列,因此只要任选x轴的点满足Xm >= Xn(m > n,m、n是标号)即可。再在所有的选择中选出最大

2020-10-18 22:44:00 115

原创 每日一题之拉低通过率 BFS leetcode 752 打开转盘锁

思路:先从最简单的0000转到0002开始。如果我要得到所有转到0002的路径,怎么转呢?先从0000转一下,有8种情况,分别为0001, 0010, 0100,1000,0009,0090,0900,9000。然后呢?再从这8种情况中旋转,得到64种情况……这样就建立起了一个决策树。遍历决策树有DFS和BFS,DFS就是回溯算法。该题要找最短路径,所以用BFS算法。第一次运行超时,查询原因是vector的查找太慢了class Solution {public: string switch

2020-10-15 10:10:46 64

原创 回溯算法小结

判断一个题能不能使用回溯算法就是看它能不能建立决策树。

2020-10-13 16:18:02 115

原创 每日一题之拉低通过率 回溯算法 leetcode 22 括号生成

思路 回溯算法套用回溯算法模板,写下结束条件和选择条件即可。class Solution {private: string path; vector<string> allPath;public: void bathTrack(string path, int count, int end, int sumR, int sumL) { if (count < 0) return; //排除掉无效括号组合 if (su

2020-10-13 15:21:25 66

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除