A - Subtle Substring Subtraction
题意:两个人比赛,给定一个字符串,Alice能选连续的偶数长度子串,Bob能选连续的奇数长度子串,Alice先选,Bob后选,每个字母有对应的得分,问谁得分高,并计算分数差值。
题解:如果字符串长度的为偶数,Alice必赢,并且获得全部分数,Bob获得不了任何分数,如果字符串长度为奇数,Alice选len-1的子串,她要么不选第一个,要么不选最后一个,两个比较下取最大,剩下的就是Bob选的一个,注意长度为1时的特判。
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
void solve() {
string s;
cin >> s;
int flag = 0;
ll sum = 0;
ll len = s.length();
if (len == 1) {
cout << "Bob" << ' ' << s[0] - 'a' + 1 << endl;
return;
}
for (int i = 0; i < len; i++) {
sum += s[i] - 'a' + 1;
}
ll a = 0;
ll b = 0;
ll sum1 = 0;
ll sum2 = 0;
if (len % 2 == 0) {
a = sum;
b = 0;
}
else {
sum1 = sum - (s[0] - 'a' + 1);
sum2 = sum - (s[len - 1] - 'a' + 1);
if (sum1 > sum2) {
a = sum1;
b = s[0] - 'a' + 1;
}
else {
a = sum2;
b = s[len - 1] - 'a' + 1;
}
}
if (a > b) flag = 1;
else flag = 0;
if (flag == 1) {
cout << "Alice" << ' ' << a - b << endl;
}
else {
cout << "Bob" << ' ' << b - a << endl;
}
}
int main() {
std::cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
}
B - A Perfectly Balanced String?
题意:给定一个字符串,任何选定的字串中,其元素出现个数之间差值小于等于1,就yes,否则,就no,例如:abcb,我选bcb作为字串,其中a出现0次,b出现2次,c出现1次,有差值大于1的,所以是no。
题解:可证当有元素出现2次时,其后的元素都应该按之前的顺序排序。
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
void solve() {
string s;
cin >> s;
int flag = 0;
ll len = s.length();
set<char>a;
int k = 0;
for (k = 0; k < len; k++) {
if (a.find(s[k]) == a.end()) a.insert(s[k]);
else break;
}
for (int i = k; i < len; i++) {
if (s[i] != s[i - k]) {
flag = 1;
break;
}
}
if (flag == 0) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main() {
std::cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
}
C - Palindrome Basis
题意:输入n,求组成n的方法有几种,其中组成的数必须是回文数。
题解:分段dp,dp[i][j]表示组成i的前j个回文数有多少种,可推转移方程:
dp[i][j]=dp[i][j-1]+dp[i-p[j]][j],p[j]为第j个回文数。
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n;
const ll mod = 1000000007;
ll dp[50005][555];
ll res = 0;
int f(int x) {
int y = 0;
int z = x;
while (x > 0) {
y = y * 10 + x % 10;
x = x / 10;
}
if (z != y) return 0;
return 1;
}
int main() {
std::cin.tie(nullptr);
int t;
cin >> t;
vector<int>v;
v.push_back(0);
for (int i = 1; i <= 40005; i++) {
if (f(i) == 1) v.push_back(i);
}
for (int i = 1; i < v.size(); i++) {
dp[0][i] = 1;
}
for (int i = 1; i < 40005; i++) {
dp[i][0] = 0;
for (int j = 1; j < v.size(); j++) {
if (v[j] <= i) dp[i][j] = (dp[i][j - 1] + dp[i - v[j]][j]) % mod;
else dp[i][j] = dp[i][j - 1];
}
}
while (t--) {
int n;
cin >> n;
cout << dp[n][v.size() - 1] << endl;
}
}