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

A. Check the string
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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'.

Output

Print "YES" or "NO", according to the condition.

Examples
input
Copy
aaabccc
output
YES
input
Copy
bbacc
output
NO
input
Copy
aabc
output
YES
Note

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");
}

猜你喜欢

转载自blog.csdn.net/qq_40731186/article/details/79867363