PAT甲级1093,1098解题报告

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

1093 Count PAT's (25 point(s))

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.

Now given any string, you are supposed to tell the number of PAT's contained in the string.

Input Specification:

Each input file contains one test case. For each case, there is only one line giving a string of no more than 10​5​​ characters containing only PA, or T.

Output Specification:

For each test case, print in one line the number of PAT's contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.

Sample Input:

APPAPT

Sample Output:

2

题目大意:输出一个字符串中含有几个PAT子串,这里的子串不一定连续。

解题思路:10的五次方,只能遍历一次,所以处理一下就是找数学规律,从头开始找,找到P记录p的次数加1,找到A就更新A的次数为p的次数加上原先A的次数,找到T就加上前面两个想乘即可。

代码如下:

#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;
vector<int> apos;
vector<int> ppos;
vector<int> tpos;
int main()
{
	string s;
	cin >> s;
	int PCount = 0;
	int PACount = 0;
	int PATCount = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i]== 'P')
		{
			PCount = PCount + 1;
		}
		if (s[i] == 'A')
		{
			PACount = PACount + PCount;
		}
		if (s[i]== 'T')
		{
			PATCount = (PATCount + PACount) % 1000000007;
		}
	}
	cout << PATCount << endl;
	return 0;
}

1098 Insertion or Heap Sort (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.

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

题目大意:判断一下是堆排序还是插入排序

解题思路:插入排序,理解成很简单的,从2个开始每次对前i个进行排列即可,堆排的话自己实现太烦了,直接用stl的heap,heap_pop的作用是可以把一个堆的最大值放到最后面,然后对剩下的进行调整。所以就是先makeheap一下,然后不断heap_pop剩下的元素,从N开始一直到1从中不断核对找到中间情况,然后再进行一次堆排输出一下就好了。

#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;
vector<int> cur;
vector<int> at;
vector<int> tmp;
void swap(int &m, int &n) {
	int tmp = m;
	m = n;
	n = tmp;
}
int main()
{
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int temp;
		scanf("%d", &temp);
		cur.push_back(temp);
		tmp.push_back(temp);
	}
	for (int i = 0; i < N; i++) {
		int temp;
		scanf("%d", &temp);
		at.push_back(temp);
	}
	bool flag = false;
	for (int i = 0;i < N; i++) {
		if (i+2<N)
			sort(cur.begin(), cur.begin()+i+2);
		else
		{
			sort(cur.begin(), cur.begin() + N);
		}
		if (at == cur) {
			flag = true;
			cout << "Insertion Sort" << endl;
			if (i + 3 < N)
				sort(cur.begin(), cur.begin() + i + 3);
			else
				sort(cur.begin(), cur.begin() + N);
			for (int i = 0; i < cur.size(); i++) {
				if (i != cur.size() - 1) {
					cout << cur[i] << " ";
				}
				else
					cout << cur[i] << endl;
			}
			break;
		}
	}
	/*for (int i = 0; i < tmp.size(); i++) {
		if (i != tmp.size() - 1) {
			cout << tmp[i] << " ";
		}
		else
			cout << tmp[i] << endl;
	}*/
	if (!flag) {
		cout << "Heap Sort" << endl;
		make_heap(tmp.begin(), tmp.end());
		for (int i = 0; i < N; i++) {
			pop_heap(tmp.begin(), tmp.end() - i);
			if (tmp == at) {
				pop_heap(tmp.begin(), tmp.end() - i - 1);
				for (int j = 0; j < tmp.size(); j++) {
					if (j!= tmp.size() - 1) {
						cout << tmp[j] << " ";
					}
					else
						cout << tmp[j] << endl;
				}
				break;
			}
		}
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值