Codeforces Round #607 (Div. 2)
比赛链接:https://codeforces.com/contest/1281
A - Suffix Three
题意
有一个字符串 $s$,要求:
- 如果 $s$ 以 "po" 结尾,输出 “FILIPINO”
- 如果 $s$ 以 "desu" 或 "masu" 结尾,输出 “JAPANESE”
- 如果 $s$ 以 "mnida" 结尾,输出 “KOREAN”
代码
#include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; int n = s.size(); string t; if (n >= 2) { t = s.substr(n - 2); if (t == "po") { cout << "FILIPINO\n"; return; } } if (n >= 4) { t = s.substr(n - 4); if (t == "desu" or t == "masu") { cout << "JAPANESE\n"; return; } } if (n >= 5) { t = s.substr(n - 5); if (t == "mnida") { cout << "KOREAN\n"; return; } } } int main() { int t; cin >> t; while (t--) solve(); }
B - Azamon Web Services
题意
有两个由大写字母组成的字符串 $s$ 和 $c$,可以交换 $s$ 中的两个字母,问 $s$ 的字典序能否严格小于 $c$ 。
题解
只需考虑 $s$ 和 $t$ 共同长度的部分:
- 如果 $s$ < $t$,输出 $s$ 即可
- 如果 $s$ ≥ $t$,变换 $s$ 中第一个可以变小的位置即可
代码
#include <bits/stdc++.h> using namespace std; void solve() { string s, c; cin >> s >> c; for (int i = 0; i < min(s.size(), c.size()); i++) { char mi = 'Z' + 1; for (int j = i + 1; j < s.size(); j++) if (s[j] < s[i]) mi = min(mi, s[j]); if (mi != 'Z' + 1) { for (int j = s.size() - 1; j >= i + 1; j--) if (s[j] == mi) { swap(s[i], s[j]); break; } break; } } if (s < c) cout << s << "\n"; else cout << "---" << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
C - Cut and Paste
题意
有一个由 1, 2, 3 组成的字符串 $s$,依次读取 $s$ 中 $[1, x]$ 位置的字符,在 $i$ 和 $i+1$ 处将 $s$ 分为两份,前一份加上 $s_i$ 个后一份后变为新的 $s$,问最终 $s$ 的长度。
题解
只需构造到长度不小于 $x$ 的 $s$ 即可,后面只对长度进行操作就可以了。
Tips
string 模拟的过程中生成的大量 string 副本会导致超时,可以使用 vector<char> 。
代码
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; void solve() { int x; cin >> x; string _s; cin >> _s; vector<char> s(_s.begin(), _s.end()); long long ans = s.size(); for (int i = 1; i <= x; i++) { int n = s[i - 1] - '1'; if (s.size() < x) { vector<char> sub(s.begin() + i, s.end()); for (int j = 0; j < n; j++) s.insert(s.end(), sub.begin(), sub.end()); } ans = (ans + (ans - i + mod) * n) % mod; } cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }

浙公网安备 33010602011771号