PAT甲級1093,1098解題報告
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 105 characters containing only P
, A
, 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;
}