您的位置:首页 > 其它

[2018-4-8]BNUZ套题比赛div2 CodeForces 960A【补】

2018-04-09 16:03 399 查看
A. Check the stringtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes).InputThe first and only line consists of a string S (1 ≤ |S| ≤ 5 000). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.OutputPrint "YES" or "NO", according to the condition.ExamplesinputCopy
aaabccc
output
YES
inputCopy
bbacc
output
NO
inputCopy
aabc
output
YES
NoteConsider first example: the number of 'c' is equal to the number of 'a'.Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.Consider third example: the number of 'c' is equal to the number of 'b'.题意: 给个字符串(只包含abc),至少包含 1个a 和 1个b,当 a的数量 b的数量等于 c的数量 时,并且字符串时按abc顺序的 是 YES题解:用 变量 a,b ,c 分别记录字符abc 的 个数;last 记录上个出现字符的ASCII码, 当前字符如果小于上个字符的ASCII码则 直接输出 NO
AC代码:#include <bits/stdc++.h>

using namespace std;

int main() {
string s;
cin >> s;
int a = 0, b = 0, c = 0, last = -1;
for (char i: s) {
if (i == 'a')
a++;
else if (i == 'b')
b++;
else
c++;
if (i < last)
goto no;
last = i;
}
if (a != 0 && b != 0 && (a == c || b == c))
puts("YES");
else
no: puts("NO");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM c 题解 CodeForces