浙大PAT题解集

2 篇文章 0 订阅

1151

  1. the lowest common ancestor is in the middle of the two nodes in the inorder sequence according to index

  2. check each element in preorder sequence can cause the running time out

    int j = 1;
    			while (j <= n)
    			{
    				if ((index_in[pre[j]] > index_in[ta] && index_in[pre[j]] < index_in[tb]) ||
    					(index_in[pre[j]] < index_in[ta] && index_in[pre[j]] > index_in[tb]))
    				{
    					printf("LCA of %d and %d is %d.\n", ta, tb, pre[j]);
    					break;
    				}
    				else if (pre[j] == ta || pre[j] == tb)
    				{
    					printf("%d is an ancestor of %d.\n", pre[j] == ta ? ta : tb, pre[j] == ta ? tb : ta);
    					break;
    				}
    				j++;
    			}
    
  3. recursive implement solve the time-out problem

  4. store the index of every element in inorder sequence as map<int, int>index_in, the map would be important, because if the value is not in index_in, the map would return 0.

Sample Input 1:

6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99

Sample Output 1:

LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.

Code

#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)

vector<int> in;
vector<int> pre;
map<int, int> index_in;
// recursive implement
void lca(int preL, int inL, int inR, int a, int b)
{
	if (inL > inR)
		return;
	int inRoot = index_in[pre[preL]], aPos = index_in[a], bPos = index_in[b];
	if (aPos < inRoot && bPos < inRoot)
		lca(preL + 1, inL, inRoot - 1, a, b);
	if((aPos<inRoot&&bPos>inRoot)|| (aPos>inRoot&&bPos<inRoot))
		printf("LCA of %d and %d is %d.\n", a, b, pre[preL]);
	else if(aPos==inRoot|| bPos == inRoot)
		printf("%d is an ancestor of %d.\n", aPos == inRoot ? a : b, aPos == inRoot ? b : a);
	if (aPos > inRoot && bPos > inRoot)
		lca(preL + inRoot - inL + 1, inRoot + 1, inR, a, b);
}

int main()
{
	int m, n, ta, tb;
	scanf("%d %d", &m, &n);
	in.resize(n+1);
	pre.resize(n+1);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &in[i]);
		index_in[in[i]] = i;
	}
	for (int i = 1; i <= n; i++)
		scanf("%d", &pre[i]);
	for (int i = 0; i < m; i++)
	{
		scanf("%d %d", &ta, &tb);
		if (index_in[ta]==0 && index_in[tb] == 0)
		{
			printf("ERROR: %d and %d are not found.\n", ta, tb);
		}
		else if (index_in[ta]==0 || index_in[tb] == 0)
		{
			printf("ERROR: %d is not found.\n", index_in[ta]==0 ? ta : tb);
		}
		else
		{
			'''/*int j = 1; //这里是超时的做法
			while (j <= n)
			{
				if ((index_in[pre[j]] > index_in[ta] && index_in[pre[j]] < index_in[tb]) ||
					(index_in[pre[j]] < index_in[ta] && index_in[pre[j]] > index_in[tb]))
				{
					printf("LCA of %d and %d is %d.\n", ta, tb, pre[j]);
					break;
				}
				else if (pre[j] == ta || pre[j] == tb)
				{
					printf("%d is an ancestor of %d.\n", pre[j] == ta ? ta : tb, pre[j] == ta ? tb : ta);
					break;
				}
				j++;
			}*/'''
			lca(1, 1, n, ta, tb);
		}
	}
	system("pause");
	return 0;
}

1152

  1. universal header file: #include<bits/stdc++.h>.
    ios::sync_with_stdio(false); cin.tie(0);make IO more efficient !
  2. s.substr(i,k)extract a substring from s, starting from index i, and there are k characters.
  3. module: isPrime(int n)
    note: check if n is 0 or 1
  4. more important: when you find a solution, output the string instead of int.

Sample Input 1:

20 5
23654987725541023819

Sample Output 1:

49877

Sample Input 2:

10 3
2468024680

Sample Output 2:

404

Code

#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;

bool isprime(int t)
{
	// 这里需要单独判断t==0和t==1的情况
	if (t == 0 || t == 1) return  false;
	for (int i = 2; i*i <= t; i++)
	{
		if (t%i == 0)
			return false;
	}
	return true;
}

int main()
{
	int l, k;
	cin >> l >> k;
	string s;
	cin >> s;
	for (int i = 0; i < l - k + 1; i++)
	{
		string t = s.substr(i, k);
		int tmp = stoi(t);
		if (isprime(tmp))
		{
			// 输出字符串即可,输出数字是不行的,即使在数字前面加了'0'
			cout << t << endl;
			system("pause");
			return 0;
		}
	}
	cout << "404" << endl;
	system("pause");
	return 0;
}

1153

There are two problems to be solved:

  1. the site must be int when output printf("Case %d: %d %d\n", i + 1, type, site);
  2. the date must be string when output printf("Case %d: %d %s\n", i + 1, type, date.c_str());
  3. traversing the map:
    unordered_map<string, int> mans;
    vector<site> ans;
    for (auto it:mans)
    {
    	// 这里的顺序first和second,要按照struct中的顺序安排
    	ans.push_back({ it.first,it.second });
    }
    

Sample Input 1:

8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

Sample Output 1:

Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

Code

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include<string>

using namespace std;
//#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)

struct node
{
	string index;
	int score;
};
struct site
{
	string index;
	int num;
};
// 排序的传参用引用,这样更快
bool cmp1(const node &a, const node &b)
{
	if (a.score != b.score)
		return a.score > b.score;
	else
		return a.index < b.index;
}
bool cmp2(const site &a, const site &b)
{
	if (a.num != b.num)
		return a.num > b.num;
	else
		return a.index < b.index;
}


int main()
{
	//ios::sync_with_stdio(false);
	//cin.tie(0);

	int n, m;
	cin >> n >> m;
	vector<node> v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i].index >> v[i].score;
	}
	for (int i = 0; i < m; i++)
	{
		int type;
		scanf("%d", &type);
		if (type == 1)
		{
			string t;
			vector<node> ans;
			cin >> t;
			for (int j = 0; j < n; j++)
			{
				if (v[j].index[0] == t[0])
					ans.push_back(v[j]);
			}
			sort(ans.begin(), ans.end(), cmp1);
			printf("Case %d: %d %s\n", i + 1, type, t.c_str());
			for (int j = 0; j < ans.size(); j++)
				printf("%s %d\n",ans[j].index.c_str(),ans[j].score);
			if (ans.size() == 0) printf("NA\n");
		}
		else if (type == 2)
		{
			int sum = 0, cnt = 0;
			int site;
			cin >> site;
			for (int j = 0; j < n; j++)
			{
				if (v[j].index.substr(1,3) == to_string(site))
				{
					cnt++;
					sum += v[j].score;
				}
			}
			// 这里输出的site必须是int,否则的话就会出错
			printf("Case %d: %d %d\n", i + 1, type, site);
			if (cnt != 0)
				printf("%d %d\n", cnt, sum);
			else
				printf("NA\n");
		}
		else if(type==3)
		{
			string date;
			cin >> date;
			unordered_map<string, int> mans;
			vector<site> ans;
			for (int j = 0; j < n; j++)
			{
				if (v[j].index.substr(4,6) == date)
				{
					string t = v[j].index.substr(1, 3);
					mans[t]++;
				}
			}
			for (auto it:mans)
			{
				// 这里的顺序first和second,要按照struct中的顺序安排
				ans.push_back({ it.first,it.second });
			}
			// 这里输出的date必须是string,否则的话就会出错
			printf("Case %d: %d %s\n", i + 1, type, date.c_str());
			sort(ans.begin(), ans.end(), cmp2);
			for (int j = 0; j < ans.size(); j++)
				printf("%s %d\n", ans[j].index.c_str(), ans[j].num);
			if (ans.size() == 0) printf("NA\n");
		}
	}
	system("pause");
	return 0;
}

1154

  1. using vector<vector<int>> to create an adjacency table
  2. fatal error: k++ type to j++ int for loop

Sample Input 1:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
4
0 1 0 1 4 1 0 1 3 0
0 1 0 1 4 1 0 1 0 0
8 1 0 1 4 1 0 5 3 0
1 2 3 4 5 6 7 8 8 9

Sample Output 1:

4-coloring
No
6-coloring
No

Code

#include<iostream>
#include<vector>
#include<set>
using namespace std;

int main()
{
	int n, m, ta, tb, k;
	cin >> n >> m;
	vector<vector<int>> all(n);
	for (int i = 0; i < m; i++)
	{
		cin >> ta >> tb;
		if (ta > tb)
		{
			int temp = ta;
			ta = tb;
			tb = temp;
		}
		all[ta].push_back(tb);
	}
	cin >> k;
	for (int i = 0; i < k; i++)
	{
		vector<int> v(n);
		set<int> s;
		int flag = 1;
		for (int j = 0; j < n; j++)
		{
			cin >> v[j];
			s.insert(v[j]);
		}
		for (int j = 0; j < n; j++)
		{
			if (flag == 0)
				break;
			// 错将k++写成了j++,考试的时候扣了10for (int k = 0; k < all[j].size(); k++)
			{
				if (v[j] == v[all[j][k]])
				{
					flag = 0;
					break;
				}
			}
		}
		if (flag == 0)
			cout << "No" << endl;
		else
			cout << s.size() << "-coloring" << endl;
	}
	system("pause");
	return 0;
}

1155

  1. complete binary tree: if the depth is k, then the depth 1 to k-1 is full, and the left of last floor is full.
  2. the index rule of level traversing: if the parent is x, then the left child is 2*x+1, the right child is 2*x+2.
  3. the last parent node index: i < (n-1)/2.
  4. the DFS algorithm: decide when to go down and when to go up level.

Sample Input 1:

8
98 72 86 60 65 12 23 50

Sample Output 1:

98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap

Sample Input 2:

8
8 38 25 58 52 82 70 60

Sample Output 2:

8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap

Sample Input 3:

8
10 28 15 12 34 9 8 56

Sample Output 3:

10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap

Code

#include<iostream>
#include<vector>
using namespace std;

int n;
vector<int> ans;
vector<int> v;
void DFS(int t)
{
	if (t > n)
	{
		return;
	}
	ans.push_back(v[t-1]);
	if (2 * t > n)
	{
		printf("%d", ans[0]);
		for (int i = 1; i < ans.size(); i++)
		{
			printf(" %d", ans[i]);
		}
		printf("\n");
		return;
	}
	for (int i = 2 * t + 1; i >= 2 * t; i--)
	{
		if (i > n)
			continue;
		DFS(i);
		ans.pop_back();
	}
}

int main()
{
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}
	DFS(1);
	int flag = v[0] > v[1] ? 1 : -1;
	for (int i = 0; i < (n - 1) / 2; i++)
	{
		int left = i * 2 + 1, right = i * 2 + 2;
		if (flag == 1 && (v[i] < v[left] || (right < n&&v[i] < v[right])))
		{
			flag = 0;
			break;
		}
		if (flag == -1 && (v[i] > v[left] || (right<n&&v[i]>v[right])))
		{
			flag = 0;
			break;
		}
	}
	if (flag == 1)
		cout << "Max Heap" << endl;
	else if (flag == -1)
		cout << "Min Heap" << endl;
	else
		cout << "Not Heap" << endl;
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值