当前位置: 首页 > 工具软件 > Solitaire4j > 使用案例 >

A. Casimir‘s String Solitaire

刘琨
2023-12-01

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Casimir has a string ss which consists of capital Latin letters 'A', 'B', and 'C' only. Each turn he can choose to do one of the two following actions:

  • he can either erase exactly one letter 'A' and exactly one letter 'B' from arbitrary places of the string (these letters don't have to be adjacent);
  • or he can erase exactly one letter 'B' and exactly one letter 'C' from arbitrary places in the string (these letters don't have to be adjacent).

Therefore, each turn the length of the string is decreased exactly by 22. All turns are independent so for each turn, Casimir can choose any of two possible actions.

For example, with ss == "ABCABC" he can obtain a string ss == "ACBC" in one turn (by erasing the first occurrence of 'B' and the second occurrence of 'A'). There are also many other options for a turn aside from this particular example.

For a given string ss determine whether there is a sequence of actions leading to an empty string. In other words, Casimir's goal is to erase all letters from the string. Is there a way to do this?

Input

The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

Each test case is described by one string ss, for which you need to determine if it can be fully erased by some sequence of turns. The string ss consists of capital letters 'A', 'B', 'C' and has a length from 11 to 5050 letters, inclusive.

Output

Print tt lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if there is a way to fully erase the corresponding string and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers).

Example

input

Copy

6
ABACAB
ABBA
AC
ABC
CABCBB
BCBCBCBCBCBCBCBC

output

Copy

NO
YES
NO
NO
YES
YES

解题说明:此题是一道字符串题,直接判断字符串中A和C字符的总数是否等于B字符的总数即可。

#include<stdio.h>
#include<string.h>

int main()
{
	char a[1000];
	int n, sum, x, j;
	scanf("%d\n", &n);
	for (int i = 1; i <= n; i++)
	{
		sum = 0;
		x = 0;
		gets(a);
		for (j = 0; a[j] != '\0'; j++)
		{
			if (a[j] == 'A' || a[j] == 'C')
			{
				sum++;
			}
			else if (a[j] == 'B')
			{
				x++;
			}
		}
		if (x == sum)
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}

 类似资料:

相关阅读

相关文章

相关问答