I. Elevator
题目大意
电梯里n个人选了m个楼层离开,你离开楼层最多能有多少人
解题思路
除了自己所在的楼层别的楼层都只离开1人即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int tt;
std::cin >> tt;
while (tt--) {
int n, m;
std::cin >> n >> m;
std::cout << n - m + 1 << "\n";
}
}
J. Similarity (Easy Version)
题目大意
n个字符串中选两个,最大化最长公共子串
解题思路
数据范围很小,暴力模拟比较即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int tt;
std::cin >> tt;
while (tt--) {
int n, ans = 0;
std::cin >> n;
std::vector<std::string> v(n);
for (int i = 0; i < n; i++) {
std::cin >> v[i];
}
std::map<std::string, std::set<int>> mp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < v[i].size(); j++) {
std::string s = "";
for (int k = j; k < v[i].size(); k++) {
s += v[i][k];
mp[s].insert(i);
}
}
}
for (auto [s, st] : mp) {
if (st.size() > 1) {
ans = std::max(ans, (int)s.size());
}
}
std::cout << ans << "\n";
}
}
H. Neil’s Machine
题目大意
给定两个字符串s和t,每次可以选择一个子串将他们都往后修改一位(a变b,z变a),求让s变成t的最小操作次数
解题思路
只需要遍历字符串,记录修正参数变化了多少次即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n, ans = 0, x = 0;
std::cin >> n;
std::string s, t;
std::cin >> s >> t;
for (int i = 0; i < n; i++) {
if ((s[i] - t[i] + 26) % 26 != x) {
ans++;
x = (s[i] - t[i] + 26) % 26;
}
}
std::cout << ans << "\n";
}
A. Today’s Word
题目大意
给定一个偶数长度的字符串,每次操作会把字符串s从中间分开为s1和s2,s1作为新字符串的开头,再加上原来的字符串s,s2全部往后偏移1位(a变b,z变a)组成新的字符串,会执行 1 0 100 10^{100} 10100次,求操作完成的后缀m位
解题思路
看似范围不可实现,但是长度是倍增的,很快就会超过m,因此只需要不断模拟长度超过m即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n, m, x;
std::cin >> n >> m;
std::string s, ss;
std::cin >> s;
x = s.back();
while (n / 2 < m) {
n = s.size();
ss = s.substr(n / 2, n / 2);
s = s.substr(0, n / 2) + s;
for (auto &ch : ss) {
ch = (ch - 'a' + 1) % 26 + 'a';
}
s = s + ss;
}
std::string ans = s.substr(s.size() - m, m);
int cnt = (x - s.back() + 16) % 26;
for (auto &ch : ans) {
ch = (ch - 'a' + cnt) % 26 + 'a';
}
std::cout << ans << "\n";
}
F. Timaeus
题目大意
初始有A朵小花,B朵小花可以合成一朵大花,有P%的概率额外得到一朵大花,有Q%的概率额外得到一朵小花,问能合成大花数量的最大期望
解题思路
概率dp,每次都分别取两种方案较大期望转移即可,特判b为1的时候,单独计算两种概率取大
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int a, b, pp, qq;
std::cin >> a >> b >> pp >> qq;
double p = pp / 100.0, q = qq / 100.0;
std::vector<double> dp(a + 1);
if (b == 1) {
std::cout << std::fixed << std::setprecision(16) << std::max(a + a * p, a / (b - q)) << "\n";
return 0;
}
for (int i = b; i <= a; i++) {
dp[i] = std::max((dp[i - b] + 1.0) * p + dp[i - b] * (1.0 - p), (dp[i - b + 1]) * q + dp[i - b] * (1.0 - q)) + 1.0;
}
std::cout << std::fixed << std::setprecision(16) << dp[a] << "\n";
}