CF BUDDY
← Problems·

1742F · Smaller

1500 · constructive algorithms, greedy, strings

Problem: Alperen has two strings, ss and tt which are both initially equal to "a".

He will perform qq operations of two types on the given strings:

  • 1    k    x1 \;\; k \;\; x — Append the string xx exactly kk times at the end of string ss. In other words, s:=s+x++xk timess := s + \underbrace{x + \dots + x}_{k \text{ times}}.
  • 2    k    x2 \;\; k \;\; x — Append the string xx exactly kk times at the end of string tt. In other words, t:=t+x++xk timest := t + \underbrace{x + \dots + x}_{k \text{ times}}.

After each operation, determine if it is possible to rearrange the characters of ss and tt such that ss is lexicographically smaller^{\dagger} than tt.

Note that the strings change after performing each operation and don't go back to their initial states.

^{\dagger} Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string pp is lexicographically smaller than string qq if there exists a position ii such that pi<qip_i < q_i, and for all j<ij < i, pj=qjp_j = q_j. If no such ii exists, then pp is lexicographically smaller than qq if the length of pp is less than the length of qq. For example, abdc<abe\texttt{abdc} < \texttt{abe} and abc<abcd\texttt{abc} < \texttt{abcd}, where we write p<qp < q if pp is lexicographically smaller than qq.

Input Format: The first line of the input contains an integer tt (1t1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains an integer qq (1q105)(1 \leq q \leq 10^5) — the number of operations Alperen will perform.

Then qq lines follow, each containing two positive integers dd and kk (1d21 \leq d \leq 2; 1k1051 \leq k \leq 10^5) and a non-empty string xx consisting of lowercase English letters — the type of the operation, the number of times we will append string xx and the string we need to append respectively.

It is guaranteed that the sum of qq over all test cases doesn't exceed 10510^5 and that the sum of lengths of all strings xx in the input doesn't exceed 51055 \cdot 10^5.

Output Format: For each operation, output "YES", if it is possible to arrange the elements in both strings in such a way that ss is lexicographically smaller than tt and "NO" otherwise.

Note: In the first test case, the strings are initially s=s = "a" and t=t = "a".

After the first operation the string tt becomes "aaa". Since "a" is already lexicographically smaller than "aaa", the answer for this operation should be "YES".

After the second operation string ss becomes "aaa", and since tt is also equal to "aaa", we can't arrange ss in any way such that it is lexicographically smaller than tt, so the answer is "NO".

After the third operation string tt becomes "aaaaaa" and ss is already lexicographically smaller than it so the answer is "YES".

After the fourth operation ss becomes "aaabb" and there is no way to make it lexicographically smaller than "aaaaaa" so the answer is "NO".

After the fifth operation the string tt becomes "aaaaaaabcaabcaabca", and we can rearrange the strings to: "bbaaa" and "caaaaaabcaabcaabaa" so that ss is lexicographically smaller than tt, so we should answer "YES".

Sample Cases

Case 1

Input

3
5
2 1 aa
1 2 a
2 3 a
1 2 b
2 3 abca
2
1 5 mihai
2 2 buiucani
3
1 5 b
2 3 a
2 4 paiu

Output

YES
NO
YES
NO
YES
NO
YES
NO
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