1059. Prime Factors (25)-PAT甲级真题(素数表的建立)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range of long int.

Output Specification:

Factor N in the format N = p1^k1 * p2^k2 *…*pm^km, where pi’s are prime factors of N in increasing order, and the exponent ki is the number of pi — hence when there is only one pi, ki is 1 and must NOT be printed out.

Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
题目大意:给出一个整数,按照从小到大的顺序输出其分解为质因数的乘法算式
分析:根号int的最大值不超过50000,先建立个50000以内的素数表,然后从2开始一直判断是否为它的素数,如果是就将a=a/i继续判断i是否为a的素数,判断完成后输出这个素数因子和个数,用state判断是否输入过因子,输入过就要再前面输出“*”

1053. Path of Equal Weight (30)-PAT甲级真题(树的遍历)

Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let’s consider the tree showed in Figure 1: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in Figure 1.

Snip20160820_53
Figure 1

Input Specification:
Each input file contains one test case. Each case starts with a line containing 0 < N <= 100, the number of nodes in a tree, M (< N), the number of non-leaf nodes, and 0 < S < 230, the given weight number. The next line contains N positive numbers where Wi (<1000) corresponds to the tree node Ti. Then M lines follow, each in the format:

ID K ID[1] ID[2] … ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 00.

Output Specification:

For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.

Note: sequence {A1, A2, …, An} is said to be greater than sequence {B1, B2, …, Bm} if there exists 1 <= k < min{n, m} such that Ai = Bi for i=1, … k, and Ak+1 > Bk+1.

Sample Input:
20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19
Sample Output:
10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2
题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径
分析:对于接收孩子结点的数据时,每次完全接收完就对孩子结点按照权值进行排序(序号变,根据权值变),这样保证深度优先遍历的时候直接输出就能输出从大到小的顺序。记录路径采取这样的方式:首先建立一个path数组,传入一个nodeNum记录对当前路径来说这是第几个结点(这样直接在path[nodeNum]里面存储当前结点的孩子结点的序号,这样可以保证在先判断return的时候,path是从0~numNum-1的值确实是要求的路径结点)。然后每次要遍历下一个孩子结点的之前,令path[nodeNum] = 孩子结点的序号,这样就保证了在return的时候当前path里面从0~nodeNum-1的值就是要输出的路径的结点序号,输出这个序号的权值即可,直接在return语句里面输出。
注意:当sum==target的时候,记得判断是否孩子结点是空,要不然如果不空说明没有到底部,就直接return而不是输出路径。。。(这对接下来的孩子结点都是正数才有用,负权无用)。

 

1045. Favorite Color Stripe (30)-PAT甲级真题(动态规划)

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva’s favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva’s favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (<=200) followed by M Eva’s favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (<=10000) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line are separated by a space.

Output Specification:

For each test case, simply print in a line the maximum length of Eva’s favorite stripe.

Sample Input:
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
7

题目大意:给出m中颜色作为喜欢的颜色(同时也给出顺序),然后给出一串长度为L的颜色序列,现在要去掉这个序列中的不喜欢的颜色,然后求剩下序列的一个子序列,使得这个子序列表示的颜色顺序符合自己喜欢的颜色的顺序,不一定要所有喜欢的颜色都出现。
分析:因为喜欢的颜色是不重复的,把喜欢的颜色的序列依次存储到数组中,book[i] = j表示i颜色的下标为j。先在输入的时候剔除不在喜欢的序列中的元素,然后把剩余的保存在数组a中。按照最长不下降子序列的方式做,对于从前到后的每一个i,如果它前面的所有的j,一下子找到了一个j的下标book[j]比book[i]小,此时就更新dp[i]使它 = max(dp[i], dp[j] + 1);并且同时再每一次遍历完成一次j后更新maxn的值为长度的最大值,最后输出maxn~

 

【数据结构】堆、堆排序笔记

【数据结构】堆、堆排序笔记

  • 堆是一棵完全二叉树,树的每个结点的值都不小于(或者不大于)其左右孩子的值。
  • 父亲结点大于等于孩子结点的值叫做大顶堆,反之叫做小顶堆
  • 大顶堆的每个结点的值都是以它为根结点的子树的最大值,反之最小值
  • 下面都以大顶堆为例子:
  • 两个兄弟之间不存在大小比较关系,堆只能说明某结点引导的子树的所有子结点都比它小,就像领导关系一样,下属之间或者领导之间可能有实力高低,但是领导是他的所有下属的最高级
  • 建堆:把所有非叶子结点都向下调整(从n/2开始直到1)
  • 向下调整:(不断和自己的左右孩子比较,把孩子的最大值和自己交换,直到结点的下标大于最后一个元素的数组下标high为止。记住:i结点的左孩子j结点满足j = i * 2;)
  • 删除堆顶元素(用最后一个元素覆盖堆顶元素,然后让n- -,然后向下调整)
  • 增加一个元素(添加到最后一个结点的后面,然后进行向上调整操作)
  • 向上调整操作就是把将要调整的结点与父亲结点比较,如果权值比父亲结点大,那么就交换其与父亲结点,这样反复比较,直到到达堆顶或者是父亲结点的权值比较大为止
  • 堆排序:
    • 在建堆完毕后,堆排序就是取出堆顶元素,然后将堆的最后一个元素i替换至堆顶,再进行一次针对堆顶元素1向下调整(1, i – 1)范围,直到堆中只有一个元素为止(i == 2)

1098. Insertion or Heap Sort (25)-PAT甲级真题(堆排序)

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 data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either “Insertion Sort” or “Heap Sort” to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
Sample Output 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
Sample Input 2:
10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9
Sample Output 2:
Heap Sort
5 4 3 1 0 2 6 7 8 9

题目大意:给出n和n个数的序列a和b,a为原始序列,b为排序其中的一个步骤,问b是a经过了堆排序还是插入排序的,并且输出它的下一步~

分析:插入排序的特点是:b数组前面的顺序是从小到大的,后面的顺序不一定,但是一定和原序列的后面的顺序相同~所以只要遍历一下前面几位,遇到不是从小到大的时候,开始看b和a是不是对应位置的值相等,相等就说明是插入排序,否则就是堆排序啦~

插入排序的下一步就是把第一个不符合从小到大的顺序的那个元素插入到前面已排序的里面的合适的位置,那么只要对前几个已排序的+后面一位这个序列sort排序即可~while(p <= n && b[p – 1] <= b[p]) p++;int index = p;找到第一个不满足条件的下标p并且赋值给index,b数组下标从1开始,所以插入排序的下一步就是sort(b.begin() + 1, b.begin() + index + 1)后的b数组~

堆排序的特点是后面是从小到大的,前面的顺序不一定,又因为是从小到大排列,堆排序之前堆为大顶堆,前面未排序的序列的最大值为b[1],那么就可以从n开始往前找,找第一个小于等于b[1]的数字b[p](while(p > 2 && b[p] >= b[1]) p–;),把它和第一个数字交换(swap(b[1], b[p]);),然后把数组b在1~p-1区间进行一次向下调整(downAdjust(b, 1,  p – 1);)~向下调整,low和high是需要调整的区间,因为是大顶堆,就是不断比较当前结点和自己的孩子结点哪个大,如果孩子大就把孩子结点和自己交换,然后再不断调整直到到达区间的最大值不能再继续了为止~

LeetCode383. Ransom Note

Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
the 
magazines,
 write 
a 
function 
that 
will 
return 
true 
if 
the 
ransom 
 note 
can 
be 
constructed 
from 
the 
magazines ; 
otherwise, 
it 
will 
return 
false.

Each 
letter
 in
 the
 magazine 
string 
can
 only 
be
 used 
once
 in
 your 
ransom
 note.

Note:
You may assume that both strings contain only lowercase letters.

canConstruct(“a”, “b”) -> false
canConstruct(“aa”, “ab”) -> false
canConstruct(“aa”, “aab”) -> true

题目大意:给两个string,判断第一个string能否由第二个string里面所含有的字母组成,第二个string里面的所有字母只能使用一次~

分析:建立一个hash数组,对第二个string遍历并记录每个字符出现的次数,然后遍历第一个string,如果有出现hash里面不存在的字符,那么return false~