CF2225B.Alternating String
入门
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let us call a string $ t $ alternating if for every $ i $ from $ 1 $ to $ n - 1 $ , the condition $ t_{i} \neq t_{i + 1} $ holds.
You are given a string $ s $ consisting only of the letters "a" and/or "b". You may perform the following operation on it at most once:
- choose a substring $ s_{l}s_{l+1}\ldots s_{r} $ consisting of at least one character;
- after choosing the substring, you may invert all letters in it, that is, change every "a" to "b" and every "b" to "a" (you are allowed to do it, but you don't have to do it);
- reverse the chosen substring, that is, transform the string $ s_{1}s_{2}\dots s_{l-1}s_{l}s_{l+1}\ldots s_{r}s_{r+1}\ldots s_{n} $ into $ s_{1}s_{2}\dots s_{l-1}s_{r}s_{r-1}\ldots s_{l}s_{r+1}\ldots s_{n} $ .
Note that when performing such an operation, you are not required to do the second step. For example, for the string $ s =~ $ "ababbab", after one operation you can obtain "abababa" by choosing the substring $ s_{5}s_{6}s_{7} $ and doing the second step, or "bababab" by choosing the substring $ s_{1}s_{2}s_{3}s_{4} $ and not doing the second step. However, you always have to perform the third step of the operation.
Your task is to determine whether it is possible to obtain any alternating string from the string $ s $ .
输入格式
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.
Each test case consists of one line containing one string $ s $ ( $ 2 \le |s| \le 2 \cdot 10^{5} $ ). The string consists only of lowercase Latin letters "a" and/or "b".
Additional constraints on the input:
- the sum of string lengths over all test cases does not exceed $ 2 \cdot 10^{5} $ .
输出格式
For each test case, output one of the following:
- YES, if it is possible to obtain an alternating string from $ s $ ;
- NO otherwise.
You may output each letter in any case.
输入输出样例
输入#1
8 abbaba aaaaa bababba ab abbaabba bbb ababa aabb
输出#1
YES NO YES YES NO YES YES YES
说明/提示
In the first example, you can choose the substring $ s_3 s_4 s_5 s_6 $ and skip the second step; then you get the string "ababab".
In the third example, you can choose the substring $ s_1 s_2 s_3 s_4 s_5 $ and perform the second step; then you get the string "abababa".
In the fourth example, the string is already alternating.
In the sixth example, you can choose the substring $ s_2 $ and perform the second step; then you get the string "bab".
输入解题思路,AI测评打分。不知道怎么写?