C. Find and Replace
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string ss consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with 00 or replace all occurrences of this character with 11.
Is it possible to perform some number of moves so that the resulting string is an alternating binary string††?
For example, consider the string abacabaabacaba. You can perform the following moves:
- Replace aa with 00. Now the string is 0b0c0b00b0c0b0.
- Replace bb with 11. Now the string is 010c010010c010.
- Replace cc with 11. Now the string is 01010100101010. This is an alternating binary string.
††An alternating binary string is a string of 00s and 11s such that no two adjacent bits are equal. For example, 0101010101010101, 101101, 11 are alternating binary strings, but 01100110, 0a0a00a0a0, 1010010100 are not.
Input
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤20001≤n≤2000) — the length of the string ss.
The second line of each test case contains a string consisting of nn lowercase Latin characters — the string ss.
Output
For each test case, output "YES" (without quotes) if you can make the string into an alternating binary string, and "NO" (without quotes) otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
Example
input
Copy
8
7
abacaba
2
aa
1
y
4
bkpt
6
ninfia
6
banana
10
codeforces
8
testcase
output
Copy
YES NO YES YES NO YES NO NO
Note
The first test case is explained in the statement.
In the second test case, the only possible binary strings you can make are 0000 and 1111, neither of which are alternating.
In the third test case, you can make 11, which is an alternating binary string.
没啥好说的,比较就行了。
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
char a[2050];
int f = 1;
cin >> n >> a;
for (int i = 0; i < n; i += 2)
for (int j = 1; j < n; j += 2)
if (a[i] == a[j])
f = 0;
if (f)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
该问题要求通过替换字符使得给定字符串变成交替的二进制字符串。输入包含多个测试用例,每个用例包括字符串长度和字符串本身。如果可能通过替换操作得到交替二进制字符串,则输出'YES',否则输出'NO'。示例中,有的字符串可以通过替换变成交替二进制字符串,有的则不能。





