A. 22222
模拟
代码实现
s = input()
print('2'*s.count('2'))
B. cat
模拟
代码实现
n = int(input())
print(''.join(sorted((input() for _ in range(n)), key=len)))
C. Debug
每找到一个 WA,将它修改成 AC,然后往后退一格,再继续找
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n-1;) {
if (s.substr(i, 2) == "WA") {
s[i] = 'A';
s[i+1] = 'C';
if (i) i--;
}
else i++;
}
cout << s << '\n';
return 0;
}
也可以从后往前找
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
for (int i = n-2; i >= 0; --i) {
if (s.substr(i, 2) == "WA") {
s[i] = 'A';
s[i+1] = 'C';
}
}
cout << s << '\n';
return 0;
}
D. Colorful Bracket Sequence
栈的基础题
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
bool solve() {
string s;
cin >> s;
stack<char> st;
auto pop = [&](char c) {
if (st.size() == 0) return false;
if (st.top() != c) return false;
st.pop();
return true;
};
for (char c : s) {
if (c == ')') {
if (!pop('(')) return false;
}
else if (c == ']') {
if (!pop('[')) return false;
}
else if (c == '>') {
if (!pop('<')) return false;
}
else {
st.push(c);
}
}
return st.size() == 0;
}
int main() {
if (solve()) puts("Yes");
else puts("No");
return 0;
}
E. Palindromic Shortest Path
记 dp[s][t] 表示从点 \(s\) 到点 \(t\) 的回文路径的最短长度
可以用多源bfs来实现
关于回文,可以从中间向两边做中心扩展
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
vector<string> c(n);
rep(i, n) cin >> c[i];
const int INF = 1001001001;
vector dist(n, vector<int>(n, INF));
queue<P> q;
auto push = [&](int s, int t, int d) {
if (dist[s][t] != INF) return;
dist[s][t] = d;
q.emplace(s, t);
};
rep(i, n) push(i, i, 0);
rep(i, n)rep(j, n) if (c[i][j] != '-') push(i, j, 1);
while (q.size()) {
auto [s, t] = q.front(); q.pop();
rep(ns, n)rep(nt, n) {
if (c[ns][s] == '-') continue;
if (c[t][nt] == '-') continue;
if (c[ns][s] != c[t][nt]) continue;
push(ns, nt, dist[s][t]+2);
}
}
rep(i, n)rep(j, n) {
int ans = dist[i][j];
if (ans == INF) ans = -1;
cout << ans << " \n"[j == n-1];
}
return 0;
}
F. Alkane
树形dp
记 dp[v] 表示以点 \(v\) 为根的子树中包含点 \(v\) 的皖烃子树的最大点数
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> to(n);
rep(i, n-1) {
int a, b;
cin >> a >> b;
--a; --b;
to[a].push_back(b);
to[b].push_back(a);
}
int ans = -1;
auto dfs = [&](auto& f, int v, int p=-1) -> int {
int res = 1;
vector<int> d;
for (int u : to[v]) {
if (u == p) continue;
d.push_back(f(f, u, v));
}
ranges::sort(d, greater<>());
if (d.size() >= 3) {
res = d[0]+d[1]+d[2]+1;
if (d.size() >= 4) {
ans = max(ans, res+d[3]);
}
}
if (d.size() >= 1) {
if (d[0]+1 > 2) ans = max(ans, d[0]+1);
}
return res;
};
dfs(dfs, 0);
cout << ans << '\n';
return 0;
}
G. Dense Buildings
其实就是求两点之间所有路径中的点权最小值的最大值
比较好的做法是整体二分+并查集,当然树剖和Kruskal重构树也行
浙公网安备 33010602011771号