PAT甲级1089,1092解题报告

49 篇文章 0 订阅
47 篇文章 0 订阅

1089 Insert or Merge (25 point(s))

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.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

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 "Merge 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 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6

题目大意:就是给两组序列,判断他是进行了什么排序,插入排序还是归并排序,并给出下一步排序后的结果

解题思路:实现一下两种排序,然后模拟一下搞一搞就好了

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
void swap(int &a, int &b) {
	int tmp = a;
	a = b;
	b = tmp;
}
vector<int> a;
vector<int> b;
vector<int> tmp;
int main()
{
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int tt;
		scanf("%d", &tt);
		a.push_back(tt);
		tmp.push_back(tt);
	}
	//vector<int> tmp;
	for (int i = 0; i < N; i++) {
		int tt;
		scanf("%d", &tt);
		b.push_back(tt);
	}
	bool flag = false;
	for (int i = 0; i < N; i++) {
		if (i + 2 < N)
			sort(a.begin(), a.begin() + i + 2);
		else
			sort(a.begin(), a.begin() + N);
		if (a == b) {
			cout << "Insertion Sort" << endl;
			if (i + 3 < N)
				sort(a.begin(), a.begin() + i + 3);
			else
				sort(a.begin(), a.begin() + N);
			for (int j = 0; j < N; j++) {
				if (j != N - 1) {
					cout << a[j] << " ";
				}
				else {
					cout << a[j] << endl;
				}
			}
			flag = true;
			break;
		}
	}
	if (!flag) {
		cout << "Merge Sort" << endl;
		for (int i = 2; i < N; i *= 2) {
			for (int j = 0; j < N; j += i) {
				if (j + i < N) {
					sort(tmp.begin() + j, tmp.begin() + j + i);
				}
				else {
					sort(tmp.begin() + j, tmp.begin() + N);
				}
				if (tmp == b) {
					for (int k = 0; k < N; k += i * 2) {
						if (k + i * 2 < N)
							sort(tmp.begin() + k, tmp.begin() + k + i * 2);
						else
							sort(tmp.begin() + k, tmp.begin() + N);
					}
					for (int m = 0; m < N; m++) {
						if (m != N - 1)cout << tmp[m] << " ";
						else
							cout << tmp[m] << endl;
					}
					break;
				}
			}
		}
	}
	return 0;
}

1092 To Buy or Not to Buy (20 point(s))

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 sell the strings in whole pieces. Hence Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is Yes, please tell her the number of extra beads she has to buy; or if the answer is No, please tell her the number of beads missing from the string.

For the sake of simplicity, let's use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. For example, the 3rd string in Figure 1 is the one that Eva would like to make. Then the 1st string is okay since it contains all the necessary beads with 8 extra ones; yet the 2nd one is not since there is no black bead and one less red bead.

figbuy.jpg

Figure 1

Input Specification:

Each input file contains one test case. Each case gives in two lines the strings of no more than 1000 beads which belong to the shop owner and Eva, respectively.

Output Specification:

For each test case, print your answer in one line. If the answer is Yes, then also output the number of extra beads Eva has to buy; or if the answer is No, then also output the number of beads missing from the string. There must be exactly 1 space between the answer and the number.

Sample Input 1:

ppRYYGrrYBR2258
YrR8RrY

Sample Output 1:

Yes 8

Sample Input 2:

ppRYYGrrYB225
YrR8RrY

Sample Output 2:

No 2

题目大意:简单理解就是两个字符串,把后面那个字符串每个字符都在前面那个找到对应,如果全部都有,那么输出yes和第一个字符串比第二个字符串大的,如果不是的话,那就输出没有的数目。

解题思路:简单模拟一下就好了,没什么难度。

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
map<char, int> a;
map<char, int> b;
int main()
{
	string c, d;
	cin >> c >> d;
	for (int i = 0; i < c.size(); i++) {
		a[c[i]] += 1;
	}
	for (int i = 0; i < d.size(); i++) {
		b[d[i]] += 1;
	}
	bool flag = true;
	int count = 0;
	for (auto i = b.begin(); i != b.end(); i++) {
		if (i->second > a[i->first]) {
			count += (i->second - a[i->first]);
			flag = false;
			//break;
		}
	}
	if (flag)
		cout << "Yes" << " " << c.size() - d.size() << endl;
	else {
		cout << "No ";
		cout << count << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值