ABC
2022 02.14
https://acs.jxnu.edu.cn/contest/24/board/challenge/A
描述:
Recently, the students of School 179 have developed a unique algorithm, which takes in a binary string ss as input. However, they soon found out that if some substring tt of ss is a palindrome of length greater than 1, the algorithm will work incorrectly. Can the students somehow reorder the characters of ss so that the algorithm will work correctly on the string?
A binary string is a string where each character is either 0 or 1.
A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
A palindrome is a string that reads the same backwards as forwards.
最近,179学校的学生开发了一种独特的算法,将二进制字符串s作为输入。然而,他们很快发现,如果s的某个子串t是长度大于1的回文,算法将无法正常工作。学生们能否以某种方式重新排列s的字符,以便算法能正确地处理字符串?
二进制字符串是每个字符为0或1的字符串。
如果aa可以通过删除开头的几个(可能是零或全部)字符和结尾的几个(可能是零或全部)字符从b中获得,则字符串a是字符串b的子字符串。
回文是一个前后读相同的字符串。
输入:
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100). Description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤1001≤n≤100) — the length of the string ss.
The second line of each test case contains the string ss of length nn consisting only of the characters 0 and 1.
输出:
For each test case, print YES (case-insensitive) if it is possible to reorder the characters of ss so that there are no substrings that are a palindrome of length greater than 1, or NO (case-insensitive) otherwise.
样例输入:
4 1 1 2 10 2 01 4 1010
复制
样例输出:
YES YES YES NO
复制
注释:
In the first three test cases, the given strings do not contain palindromes of length greater than 1, so the answers are YES.
In the last test case, it is impossible to reorder the characters so that the string does not contain palindromes of length greater than 1, so the answer is NO.
在前三个测试用例中,给定的字符串不包含长度大于1的回文,因此答案是肯定的。
在最后一个测试用例中,不可能对字符进行重新排序,以便字符串不包含长度大于1的回文,因此答案是否定的。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char b[10001];
int main(){
int n;
cin>>n;
while(n--){
int a;
memset(b,0,sizeof(b));
scanf("%d",&a);
scanf("%s",b);
if(a>2||b[0]==b[1]){
printf("NO\n");
}
else{
printf("YES\n");
}
}
return 0;
}