TUNIVERSE

PAT甲级2018春真题

字数统计: 874阅读时长: 5 min
2023/09/10

PAT考试真题解析

[1140] Look-and-say Sequence (20)

字符串

题目

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
string s;
int n, j;
cin >> s >> n;
for (int cnt = 1; cnt < n; cnt++) {
string t;
for (int i = 0; i < s.length(); i = j) {
for (j = i; j < s.length() && s[j] == s[i]; j++);
t += s[i] + to_string(j - i);
}
s = t;
}
cout << s;
return 0;
}

[1141] PAT Ranking of Institutions (25)

哈希、排序

题目

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
unordered_map<string, double> weighted_score;
unordered_map<string, int> num;

int main() {
double score;
int rank = 0, n;
cin >> n;
string uid, school;
for (int i = 0; i < n; i++) {
cin >> uid >> score >> school;
for (int j = 0; j < school.length(); j++)
school[j] = tolower(school[j]);
num[school]++;
if (uid[0] == 'A') {
weighted_score[school] += score;
} else if (uid[0] == 'T') {
weighted_score[school] += score * 1.5;
} else {
weighted_score[school] += score * 1.0 / 1.5;
}
}

vector<pair<string, int>> ans;
for (auto it = weighted_score.begin(); it != weighted_score.end(); it++)
ans.push_back(make_pair(it -> first, (int)(it -> second)));
sort(ans.begin(), ans.end(), [&](auto a, auto b) {
return a.second == b.second? (num[a.first] == num[b.first]? a.first < b.first: num[a.first] < num[b.first]): a.second > b.second;
});

cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
if (i && ans[i].second == ans[i-1].second) {}
else rank = i + 1;
printf("%d %s %d %d\n", rank, ans[i].first.c_str(), (int)ans[i].second, num[ans[i].first]);
}
return 0;
}

[1142] Maximal Clique (25)

图论

题目

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
vector<vector<int>> G;
vector<int> vis;

int judge(vector<int> &clique, int n) {
for (int i = 0; i < clique.size() - 1; i++) {
for (int j = i + 1; j < clique.size(); j++) {
if (G[clique[i]][clique[j]] != 1)
return 0;
}
}
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
int cnt = 0;
for (auto q: clique)
if (G[i][q] == 1) cnt++;
if (cnt == clique.size()) return 1;
}
return 2;
}

int main() {
int n, m, a, b, k;
cin >> n >> m;
G.resize(n + 1, vector<int>(n + 1));
vis.resize(n + 1);
for (int i = 0; i < m; i++) {
cin >> a >> b;
G[a][b] = 1;
G[b][a] = 1;
}
cin >> k;
for (int i = 0; i < k; i++) {
cin >> a;
vector<int> clique;
fill(vis.begin(), vis.end(), false);
for (int j = 0; j < a; j++) {
cin >> b;
clique.push_back(b);
vis[b] = true;
}
int ans = judge(clique, n);
if (ans == 2) printf("Yes\n");
else if (ans == 1) printf("Not Maximal\n");
else printf("Not a Clique\n");
}
return 0;
}

[1143] Lowest Common Ancestor (30)

题目

注意点与解析

map<int, bool> mp ⽤来标记树中所有出现过的结点,遍历⼀遍pre数组,将当前结点标记为a,如果u和v
分别在a的左、右,或者u、v其中⼀个就是当前a,即 (a >= u && a <= v) || (a >= v && a <= u),说明找到了这个共同最低祖先a,退出当前循环,最后根据要求输出结果即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
map<int, bool> mp;
int main() {
int m, n, u, v, a;
scanf("%d %d", &m, &n);
vector<int> pre(n);
for (int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
mp[pre[i]] = true;
}
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
for(int j = 0; j < n; j++) {
a = pre[j];
if ((a >= u && a <= v) || (a >= v && a <= u)) break;
}
if (mp[u] == false && mp[v] == false)
printf("ERROR: %d and %d are not found.\n", u, v);
else if (mp[u] == false || mp[v] == false)
printf("ERROR: %d is not found.\n", mp[u] == false ? u : v);
else if (a == u || a == v)
printf("%d is an ancestor of %d.\n", a, a == u ? v : u);
else
printf("LCA of %d and %d is %d.\n", u, v, a);
}
return 0;
}
CATALOG
  1. 1. [1140] Look-and-say Sequence (20)
    1. 1.1. 题目
    2. 1.2. 代码
  2. 2. [1141] PAT Ranking of Institutions (25)
    1. 2.1. 题目
    2. 2.2. 代码
  3. 3. [1142] Maximal Clique (25)
    1. 3.1. 题目
    2. 3.2. 代码
  4. 4. [1143] Lowest Common Ancestor (30)
    1. 4.1. 题目
    2. 4.2. 注意点与解析
    3. 4.3. 代码