目录
题目:
You are given a string ss. You have to determine whether it is possible to build the string ss out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.
For example:
- aaaabbb can be built as aa ++ aa ++ bbb;
- bbaaaaabbb can be built as bb ++ aaa ++ aa ++ bbb;
- aaaaaa can be built as aa ++ aa ++ aa;
- abab cannot be built from aa, aaa, bb and/or bbb.
输入:
The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
Each test case consists of one line containing the string ss (1≤|s|≤501≤|s|≤50), consisting of characters a and/or b.
输出:
For each test case, print YES if it is possible to build the string ss. Otherwise, print NO.
You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
Examples
input
8
aaaabbb
bbaaaaabbb
aaaaaa
abab
a
b
aaaab
bbaaa
output
YES
YES
YES
NO
NO
NO
NO
YES
代码:
#include <iostream>
#include <string>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int, int> PII;
int t;
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> s;
int f = 1;
int i = 0;
int n = s.size();
int ans = 0;
while (i < n) {
int j = i;
while (s[j] == s[j + 1])
j++;
if ((j - i + 1) < 2) { //如果连续相同的数目小于则2不能
cout << "NO" << endl;
f = 0;
break;
}
i = j + 1;
}
if (f) {
cout << "YES" << endl;
}
}
return 0;
}