PAT 甲级
haohao0626
这个作者很懒,什么都没留下…
展开
-
1118 Birds in Forest (25分) 简单并查集
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of b原创 2020-07-23 17:29:48 · 149 阅读 · 0 评论 -
1002 A+B for Polynomials (25分) 模拟 简单但是有坑题
This time, you are supposed to findA+BwhereAandBare two polynomials.Input Specification:Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:KN1aN1N2aN2...原创 2020-07-13 11:35:23 · 631 阅读 · 0 评论 -
1001 A+B Format (20分) 模拟题 输出格式
Calculatea+band 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 Specification:Each input file contains one test case. Each case contains a pair o..原创 2020-07-04 22:49:23 · 180 阅读 · 0 评论 -
1151 LCA in a Binary Tree (30分) LCA 树的遍历 技巧型
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.Given any two nodes in a binary tree, you are supposed to find their LCA.Input Specification:Each input file contains one test ca原创 2020-07-02 18:13:54 · 262 阅读 · 0 评论 -
1146 Topological Order (25分) 拓扑序列的判断 拓扑排序
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.Input Specification:Each inpu原创 2020-07-02 11:47:18 · 584 阅读 · 0 评论 -
1139 First Contact (30分) 模拟题 有点像并查集,但可以不用并查集的方法
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a girl B, he would usually not contact her directly in the first place. Instead, he might ask another boy C,原创 2020-07-02 10:27:15 · 296 阅读 · 0 评论 -
1135 Is It A Red-Black Tree (30分) 红黑树 平衡二叉树
There is a kind of balanced binary search tree namedred-black treein the data structure. It has the following 5 properties:(1) Every node is either red or black. (2) The root is black. (3) Every leaf (NULL) is black. (4) If a node is red, then both ..原创 2020-07-02 22:32:26 · 315 阅读 · 0 评论 -
1131 Subway Map (30分) DFS 深度优先搜索 怪麻烦的 太美了
In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing subway. Now you are supposed to help people with your computer skills! Given the starting position of your u原创 2020-07-01 18:38:52 · 252 阅读 · 0 评论 -
1123 Is It a Complete AVL Tree (30分) AVL 平衡二叉搜索树 层序遍历
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate t原创 2020-06-30 23:25:39 · 196 阅读 · 0 评论 -
1119 Pre- and Post-order Traversals (30分) 前后序遍历写出中序遍历 树的遍历
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and原创 2020-06-30 21:27:32 · 234 阅读 · 0 评论 -
1115 Counting Nodes in a BST (30分) 建树 BST搜索二叉树 建树要小心可能不是完全二叉树
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than or equal to the node's key. The right subtree of a node contains only nodes with key原创 2020-06-29 23:23:36 · 146 阅读 · 0 评论 -
1111 Online Map (30分) dijkstra算法 加复杂条件的最短路径+DFS 路径保存
Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.Input Spe原创 2020-06-29 22:39:48 · 137 阅读 · 0 评论 -
1107 Social Clusters (30分) 并查集 模板题 但不能大意
When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. Asocial clusteris a set of people who have some of their hobbies in common. You are supposed to find all the cl..原创 2020-06-29 15:50:03 · 146 阅读 · 0 评论 -
1103 Integer Factorization (30分) DFS深度优先搜索 经典递归
TheK−Pfactorization of a positive integerNis to writeNas the sum of theP-th power ofKpositive integers. You are supposed to write a program to find theK−Pfactorization ofNfor any positive integersN,KandP.Input Specification:Each input ...原创 2020-06-28 18:41:15 · 128 阅读 · 0 评论 -
1102 Invert a Binary Tree (25分) 树的遍历 层序和中序遍历
The following is from Max Howell @twitter:Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.Now it's your turn to prove that YOU CAN invert a binary tree!Input Specifi原创 2020-06-27 23:18:20 · 139 阅读 · 0 评论 -
1101 Quick Sort (25分) 快速排序 快排 思维
There is a classical process namedpartitionin the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. GivenN...原创 2020-06-27 11:04:56 · 200 阅读 · 0 评论 -
1098 Insertion or Heap Sort (25分) 快速排序 堆排序
According to Wikipedia:Insertion sortiterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, a.原创 2020-06-27 00:19:28 · 180 阅读 · 0 评论 -
1091 Acute Stroke (30分) BFS广度优先搜索
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.Input Specifica原创 2020-06-26 22:16:52 · 169 阅读 · 0 评论 -
1072 Gas Station (30分) dijkstar算法 复杂化:多次Dijkstra+带些附加的计算要求 经典
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.Now given the map of the原创 2020-06-26 11:32:43 · 213 阅读 · 0 评论 -
1068 Find More Coins (30分) 01背包问题 动态规划
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for eac原创 2020-06-25 10:33:10 · 141 阅读 · 0 评论 -
1066 Root of AVL Tree (25分) 平衡二叉树 经典递归
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate t原创 2020-06-25 00:27:53 · 180 阅读 · 0 评论 -
1064 Complete Binary Search Tree (30分) 完全二叉搜索树 层序遍历 递归 经典好题
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater th原创 2020-06-24 18:19:14 · 180 阅读 · 0 评论 -
1043 Is It a Binary Search Tree (25分) 二叉搜索树 遍历 恶心题
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater th原创 2020-06-21 23:25:10 · 582 阅读 · 0 评论 -
1155 Heap Paths (30分) HEAP 深搜回溯 堆 DFS 保存路径
In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t..原创 2020-06-17 22:13:32 · 119 阅读 · 0 评论 -
1154 Vertex Coloring (25分) 常规题 逻辑麻烦些 SET 算简单题
Aproper vertex coloringis a labeling of the graph's vertices with colors such that no two vertices sharing the same edge have the same color. A coloring using at mostkcolors is called a (proper)k-coloring.Now you are supposed to tell if a given colo...原创 2020-06-16 19:47:53 · 129 阅读 · 0 评论 -
1150 Travelling Salesman Problem (25分) 图论
The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?" It is an NP-hard problem in com原创 2020-06-14 10:25:32 · 187 阅读 · 0 评论 -
1147 Heaps (30分) 堆 树的遍历 层序转后序
In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t..原创 2020-06-13 22:08:50 · 284 阅读 · 0 评论 -
1145 Hashing - Average Search Time (25分) hash 哈希 查找的时候为何边界范围为tsize 待查 看看课本
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether原创 2020-06-13 19:01:53 · 136 阅读 · 0 评论 -
1143 Lowest Common Ancestor (30分) 注意平衡二叉树的特性(大小关系) 要巧妙利用这一点
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.A binary search tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node co原创 2020-06-13 16:50:37 · 154 阅读 · 0 评论 -
1142 Maximal Clique (25分) 图论 模拟
Acliqueis a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. Amaximal cliqueis a clique that cannot be extended by including one more adjacent vertex. (Quoted fromhttps://en.wikipedia.org/wiki...原创 2020-06-13 13:32:04 · 180 阅读 · 0 评论 -
1138 Postorder Traversal (25分) 遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the corresponding binary tree.Input Specific原创 2020-06-12 22:16:46 · 135 阅读 · 0 评论 -
1134 Vertex Cover (25分) hash 模拟
Avertex coverof a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. Now given a graph with several vertex sets, you are supposed to tell if each of them is a vertex cover or not.Input Specificati..原创 2020-06-12 16:33:30 · 133 阅读 · 0 评论 -
1127 ZigZagging on a Tree (30分) 中序后序建树 层次遍历
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However原创 2020-07-01 16:20:25 · 178 阅读 · 0 评论 -
1130 Infix Expression (25分) DFS 树的中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.Input Specification:Each input file contains one test case. For each case, the first line gives a原创 2020-06-10 17:00:09 · 145 阅读 · 0 评论 -
1122 Hamiltonian Cycle (25分) 图 模拟
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle".In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle.Input Specification:Each inpu原创 2020-06-05 11:08:41 · 145 阅读 · 0 评论 -
1110 Complete Binary Tree (25分) 完全二叉树 遍历
Given a tree, you are supposed to tell if it is a complete binary tree.Input Specification:Each input file contains one test case. For each case, the first line gives a positive integerN(≤20) which is the total number of nodes in the tree -- and henc..原创 2020-06-03 22:31:58 · 194 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30分) 树的遍历 搜索二叉树
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater th原创 2020-06-01 17:24:17 · 341 阅读 · 0 评论 -
1106 Lowest Price in Supply Chain (25分) 树的遍历 DFS OR BFS 这道题用bfs比较好 只要找到第一个满足条件的一层就可以了
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on the chain buys products from one's supplier in a pricePa..原创 2020-05-30 10:13:52 · 179 阅读 · 0 评论 -
1097 Deduplication on a Linked List (25分) 链表 技巧型
Given a singly linked listLwith integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each valueK, only the first node of which the value or absolute value of its key equalsKwill be kept. At the m...原创 2020-05-29 23:16:11 · 184 阅读 · 0 评论 -
1094 The Largest Generation (25分) 树的遍历 DFS OR BFS
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.Input Specification:Each input file contains one test case.原创 2020-05-29 18:31:42 · 118 阅读 · 0 评论