- 博客(142)
- 收藏
- 关注
原创 Maximum Gap 寻找数组中排序后相邻两个数的最大差值,桶排序,O(n)
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements
2015-09-07 14:53:36 3296
原创 Word Break II 字符串分割 动态规划+DFS
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "
2015-09-02 16:29:19 1451
原创 Word Ladder II 相邻字符串接龙 BFS
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word must exi
2015-09-01 10:55:04 1497
原创 N-Queens N皇后问题 DFS
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.Eac
2015-08-18 20:11:28 1076
原创 目录树的建立和输出
在ZIP归档文件中,保留着所有压缩文件和目录的相对路径和名称。当使用WinZIP等GUI软件打开ZIP归档文件时,可以从这些信息中重建目录的树状结构。请编写程序实现目录的树状结构的重建工作。输入格式说明:输入首先给出正整数N(4),表示ZIP归档文件中的文件和目录的数量。随后N行,每行有如下格式的文件或目录的相对路径和名称(每行不超过260个字符):1) 路径和名称中的字符
2015-08-09 14:29:31 1715
原创 中缀表达式转后缀表达式
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。输入格式说明:输入在一行中给出不含空格的中缀表达式,可包含+、-、*、\以及左右括号(),表达式不超过20个字符。输出格式说明:在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分
2015-08-08 13:33:42 2447
原创 海盗分赃
P个海盗偷了D颗钻石后来到公海分赃,一致同意如下分赃策略:首先,P个海盗通过抽签决定1-P的序号。然后由第1号海盗提出一个分配方案(方案应给出每个海盗分得的具体数量),如果能够得到包括1号在内的绝对多数(即大于半数)同意,则按照该分配方案执行,否则1号将被投入大海喂鲨鱼;而后依次类似地由第2号、第3号等等海盗提出方案,直到能够获得绝对多数同意的方案出现为止,或者只剩下最后一位海盗,其独占所
2015-08-05 20:18:39 1443
原创 扑克牌算24点 DFS
一副扑克牌的每张牌表示一个数(J、Q、K分别表示11、12、13,两个司令都表示6)。任取4张牌,即得到4个1~13的数,请添加运算符(规定为加+ 减- 乘* 除/ 四种)使之成为一个运算式。每个数只能参与一次运算,4个数顺序可以任意组合,4个运算符任意取3个且可以重复取。运算遵从一定优先级别,可加括号控制,最终使运算结果为24。请输出一种解决方案的表达式,用括号表示运算优先。如果没有一种解决方案
2015-08-05 18:56:32 1401
原创 移动拼图游戏(八数码问题)A*版
小时候玩过的移动拼图游戏。有一个3*3的棋盘,其中有0-8这9个数字,0表示空格,其他的数字可以和0交换位置。求从初始状态2 3 07 1 65 8 4变到目标状态1 2 34 5 67 8 0的最佳移动方案。第一次写A*算法,搞了一下午。。。。。在上一篇博客BFS版的基础上,为每个结点添加g、h、f三个变量,
2015-08-04 17:06:41 4228
原创 移动拼图游戏(八数码问题) BFS版
小时候玩过的移动拼图游戏。有一个3*3的棋盘,其中有0-8这9个数字,0表示空格,其他的数字可以和0交换位置。求从初始状态2 3 07 1 65 8 4变到目标状态1 2 34 5 67 8 0的最佳移动方案。今天先用BFS练练手。每个结点用一个长度为9的数组记录当前矩阵;用blankPos变量记录空格位置,便于计算出相邻结点;用parent指针
2015-08-03 22:45:15 2945
原创 KMP字符串匹配算法
今天学习了一下KMP字符串匹配算法。看的是July的博客从头到尾彻底理解KMP(2014年8月22日版),写得很详细。有两个字符串,母串S,子串P,要在S中找到P。朴素的字符串匹配算法是一一比较两个字符串中的每一个字符,同时记录连续匹配的长度,该长度与P长度一致时说明在S中找到了P。若某个字符失配,则S的索引i和P的索引j都要回退已匹配的长度,然后以下一个字符为首字符继续匹配。时间复杂度为O(
2015-08-03 14:18:24 542
原创 PAT (Advanced Level) 1099. Build A Binary Search Tree (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 the node's key.The right
2015-08-01 14:05:41 772
原创 PAT (Advanced Level) 1098. Insertion or Heap Sort (25) 直插和堆排
According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input dat
2015-08-01 10:31:01 1003
原创 PAT (Advanced Level) 1097. Deduplication on a Linked List (25) 链表去重
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or a
2015-08-01 09:35:35 844
原创 PAT (Advanced Level) 1096. Consecutive Factors (20) 连续质因数
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given
2015-07-31 21:16:23 2010
原创 PAT (Advanced Level) 1095. Cars on Campus (30) 结构体排序,合并区间
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you a
2015-07-31 20:38:43 1072
原创 PAT (Advanced Level) 1094. The Largest Generation (25) 人数最多的一代,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.Inpu
2015-07-31 15:56:50 514
原创 PAT (Advanced Level) 1093. Count PAT's (25) 一维动态规划
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.No
2015-07-31 15:26:45 589
原创 PAT (Advanced Level) 1092. To Buy or Not to Buy (20) 哈希
Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sel
2015-07-31 14:56:15 701
原创 PAT (Advanced Level) 1091. Acute Stroke (30) 3维DFS(10M数据时栈溢出)、3维BFS(AC)
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 c
2015-07-31 10:05:33 1119
原创 PAT (Advanced Level) 1090. Highest Price in Supply Chain (25) 供应链,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
2015-07-31 09:02:10 942
原创 PAT (Advanced Level) 1089. Insert or Merge (25) 插入和归并
According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input dat
2015-07-30 21:50:22 753
原创 PAT (Advanced Level) 1088. Rational Arithmetic (20) 分数四则运算
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.Input Specification:Each input file contains one te
2015-07-30 21:32:05 1198
原创 PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.Input Specification:E
2015-07-30 20:32:40 998
原创 PAT (Advanced Level) 1086. Tree Traversals Again (25) 根据先根遍历数组创建树
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stac
2015-07-30 15:56:36 534
原创 PAT (Advanced Level) 1085. Perfect Sequence (25) 贪心算法
Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M Now given a sequence and a parameter p, you are supposed to find from the
2015-07-30 15:17:32 558
原创 PAT (Advanced Level) 1084. Broken Keyboard (20) 坏键盘,字符串处理
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.Now given a string that you are supposed
2015-07-30 14:42:04 363
原创 PAT (Advanced Level) 1083. List Grades (25) 结构体排序
Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades
2015-07-30 14:23:30 419
原创 PAT (Advanced Level) 1082. Read Number in Chinese (25) 数字转汉字大写
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san B
2015-07-30 13:48:16 796
原创 PAT (Advanced Level) 1081. Rational Sum (20) 分数求和,辗转相除法求gcd
Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum.Input Specification:Each input file contains one test case. Each case starts with a positi
2015-07-30 11:04:25 615
原创 PAT (Advanced Level) 1080. Graduate Admission (30) 模拟高考录取,结构体排序
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admissio
2015-07-30 10:18:33 595
原创 PAT (Advanced Level) 1079. Total Sales of Supply Chain (25) 总叠加售价 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
2015-07-29 22:08:22 695
原创 PAT (Advanced Level) 1078. Hashing (25) 哈希,二次探测法
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key %
2015-07-29 21:12:19 583
原创 PAT (Advanced level) 1077. Kuchiguse (20) 最长共同后缀
The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called
2015-07-29 20:52:43 380
原创 PAT (Advanced Level) 1076. Forwards on Weibo (30) 微博转发 BFS
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a
2015-07-29 20:12:20 442
原创 PAT (Advanced Level) 1075. PAT Judge (25) PAT排名 结构体排序
The ranklist of PAT is generated from the status list, which shows the scores of the submittions. This time you are supposed to generate the ranklist for PAT.Input Specification:Each input fil
2015-07-29 19:21:44 659
原创 PAT (Advanced Level) 1074. Reversing Linked List (25) 翻转链表
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K
2015-07-29 16:23:27 525
原创 PAT (Advanced Level) 1073. Scientific Notation (20) 科学计数法
Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the in
2015-07-29 15:46:18 481
原创 PAT (Advanced Level) 1072. Gas Station (30) 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 hou
2015-07-29 14:48:30 899
原创 PAT (Advanced Level) 1071. Speech Patterns (25) 字符串处理
People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker
2015-07-29 10:30:51 494
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人