2202B.md (2717B)
1 Source: https://codeforces.com/problemset/problem/2202/B 2 3 B. ABAB Construction 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 There is a string $T$ of length $n$, such that $T_i=$ 'a' for all odd $i$ and $T_i=$ 'b' for all even $i$. 22 23 One day, Bob generated a string $S$ with the following algorithm. 24 25 1. Initialize $S$ to the empty string. 26 2. Remove either the first letter or the last letter from $T$, and append it to $S$. 27 3. If $T$ is empty, terminate and return the string $S$. Otherwise, go back to the second step. 28 29 Then, Bob wrote down the generated string $S$ on a note and forgot about it for a few years. The note was worn out when Bob found it, and someone might have secretly changed some letters. Of course, Bob wants to know if someone did alter the string! 30 31 You are given a string $X$ of length $n$, which consists of 'a', 'b', and '?'. 32 33 Please determine if there exists a string $A$ which satisfies the following conditions: 34 35 * $|A|=n$; 36 * $A_i$ is either 'a' or 'b' for all $1 \le i \le n$; 37 * $A_i=X_i$ for all $1 \le i \le n$ such that $X_i$ is not '?'; 38 * The string $A$ can be generated from the algorithm described above. 39 40 Input 41 42 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 43 44 The first line of each test case contains a single integer $n$ ($1 \le n \le 200\,000$). 45 46 The second line of each test case contains a string $X$ of length $n$, consisting of 'a', 'b', and '?'. 47 48 It is guaranteed that the sum of $n$ over all test cases does not exceed $200\,000$. 49 50 Output 51 52 If there is a string $A$ that satisfies all conditions, output "YES" on a separate line. 53 54 If there is no string $A$ that satisfies all conditions, output "NO" on a separate line. 55 56 You can output the answer in any case. For example, the strings "yEs", "yes", "Yes" will also be recognized as positive responses. 57 58 Example 59 60 Input 61 62 ``` 63 4 64 65 5 66 67 ababa 68 69 5 70 71 baaba 72 73 5 74 75 ?b?ab 76 77 6 78 79 aa?b?b 80 ``` 81 82 Output 83 84 ``` 85 YES 86 87 NO 88 89 YES 90 91 NO 92 ``` 93 94 Note 95 96 For the second test case, the string "baaba" cannot be generated by the algorithm described above. 97 98 For the third test case, the string "abaab" can be generated by the algorithm described above: 99 100 1. Initially, $T$ is "ababa" and $S$ is empty. 101 2. Remove the first letter from $T$. $T$ becomes "baba" and $S$ becomes "a". 102 3. Remove the first letter from $T$. $T$ becomes "aba" and $S$ becomes "ab". 103 4. Remove the last letter from $T$. $T$ becomes "ab" and $S$ becomes "aba". 104 5. Remove the first letter from $T$. $T$ becomes "b" and $S$ becomes "abaa". 105 6. Remove the last letter from $T$. $T$ becomes empty and $S$ becomes "abaab".