A 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).
The 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'.
Print "YES" or "NO", according to the condition.
aaabccc
YES
bbacc
NO
aabc
YES
Consider 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"); }