A. Deletions of Two Adjacent Letters
题目描述:给一串字符串s和一个字符c,判断该字符串能否在被若干次拿走连续的两个字符后只剩下c字符。本题的核心在于连续的两个字符。
思路:本题的思路不是很难,我们只需要判断s中的c字符前有多少字符即可,若字符数目为偶数或0,说明可以变成c;若字符数目为奇数,则不能变成c。
#include<cstdio>
#include<iostream>
using namespace std;
int main() {
int n = 0;
cin >> n;
while (n--) {
string s;
char c;
cin >> s >> c;
int siz = s.size();
int cnt = 0;
bool isok = false;
if (siz == 1) {
if (s[0] == c)
printf("YES\n");
else printf("NO\n");
continue;
}
else {
for (int i = 0; i < siz; i++) {
if (s[i] == c) {//找到c字符
if (!(cnt % 2)) {
isok = true;//如果前面的元素个数是偶数就Ok
continue;
}
}
cnt++;
}
if (isok)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
B. DIV + MOD
题目描述:本题的意思就是,给定l,r,a,在l-r的区间中选择一个x,使得x/a+x%a最大
思路:不难发现,x%a的值其实是一个周期函数,值域为0-a-1,而x/a则是当x每增大a的一倍,值就增大1,因此我们可以发现,最大值其实只会在右端点和最接近r的a的最大倍数-1的点处产生,我们只需要选出这两点之中更大的值就可以了。(其实就是观察x/a和x%a这两个函数的性质)
#include<cstdio>
#include<iostream>
using namespace std;
int l, r, a;
int t = 0;
int main() {
cin >> t;
while (t--) {
cin >> l >> r >> a;
int res = 0;
int ans1 = r / a + r % a;//右端点运算
int ans2 = 0;
r = r - r % a;//最接近r且能整除a的最大整数
if (l <= r - 1) ans2 = (r - 1) / a + a - 1;
res = max(ans1, ans2);
printf("%d\n", res);
}
return 0;
}
C. Weight of the System of Nested Segments
明天写。