CF BUDDY
← Problems·

1685B · Linguistics

2000 · greedy, implementation, sortings

Problem: Alina has discovered a weird language, which contains only 44 words: A\texttt{A}, B\texttt{B}, AB\texttt{AB}, BA\texttt{BA}. It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.

Alina has found one such sentence ss and she is curious: is it possible that it consists of precisely aa words A\texttt{A}, bb words B\texttt{B}, cc words AB\texttt{AB}, and dd words BA\texttt{BA}?

In other words, determine, if it's possible to concatenate these a+b+c+da+b+c+d words in some order so that the resulting string is ss. Each of the a+b+c+da+b+c+d words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.

Input Format: The first line of the input contains a single integer tt (1t1051 \le t \le 10^5) — the number of test cases. The description of the test cases follows.

The first line of each test case contains four integers aa, bb, cc, dd (0a,b,c,d21050\le a,b,c,d\le 2\cdot 10^5) — the number of times that words A\texttt{A}, B\texttt{B}, AB\texttt{AB}, BA\texttt{BA} respectively must be used in the sentence.

The second line contains the string ss (ss consists only of the characters A\texttt{A} and B\texttt{B}, 1s21051\le |s| \le 2\cdot 10^5, s=a+b+2c+2d|s|=a+b+2c+2d)  — the sentence. Notice that the condition s=a+b+2c+2d|s|=a+b+2c+2d (here s|s| denotes the length of the string ss) is equivalent to the fact that ss is as long as the concatenation of the a+b+c+da+b+c+d words.

The sum of the lengths of ss over all test cases doesn't exceed 21052\cdot 10^5.

Output Format: For each test case output YES\texttt{YES} if it is possible that the sentence ss consists of precisely aa words A\texttt{A}, bb words B\texttt{B}, cc words AB\texttt{AB}, and dd words BA\texttt{BA}, and NO\texttt{NO} otherwise. You can output each letter in any case.

Note: In the first test case, the sentence ss is B\texttt{B}. Clearly, it can't consist of a single word A\texttt{A}, so the answer is NO\texttt{NO}.

In the second test case, the sentence ss is AB\texttt{AB}, and it's possible that it consists of a single word AB\texttt{AB}, so the answer is YES\texttt{YES}.

In the third test case, the sentence ss is ABAB\texttt{ABAB}, and it's possible that it consists of one word A\texttt{A}, one word B\texttt{B}, and one word BA\texttt{BA}, as A+BA+B=ABAB\texttt{A} + \texttt{BA} + \texttt{B} = \texttt{ABAB}.

In the fourth test case, the sentence ss is ABAAB\texttt{ABAAB}, and it's possible that it consists of one word A\texttt{A}, one word AB\texttt{AB}, and one word BA\texttt{BA}, as A+BA+AB=ABAAB\texttt{A} + \texttt{BA} + \texttt{AB} = \texttt{ABAAB}.

In the fifth test case, the sentence ss is BAABBABBAA\texttt{BAABBABBAA}, and it's possible that it consists of one word A\texttt{A}, one word B\texttt{B}, two words AB\texttt{AB}, and two words BA\texttt{BA}, as BA+AB+B+AB+BA+A=BAABBABBAA\texttt{BA} + \texttt{AB} + \texttt{B} + \texttt{AB} + \texttt{BA} + \texttt{A}= \texttt{BAABBABBAA}.

Sample Cases

Case 1

Input

8
1 0 0 0
B
0 0 1 0
AB
1 1 0 1
ABAB
1 0 1 1
ABAAB
1 1 2 2
BAABBABBAA
1 1 2 3
ABABABBAABAB
2 3 5 4
AABAABBABAAABABBABBBABB
1 3 3 10
BBABABABABBBABABABABABABAABABA

Output

NO
YES
YES
YES
YES
YES
NO
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback