#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while(T --) {
string s, t;
cin >> s;
int res = 0;
for(int i = 0;i < 7;i ++) {
if(s[i] == s[i+1] && s[i+1] == s[i+2]) {
res ++;
for(int j = 0;j < i;j ++) t = t + s[j];
for(int j = i + 3;j < 9;j ++) t = t + s[j];
break;
}
}
if(res) {
// cout <<t << endl;
s = t;
t.clear();
for(int i = 0;i < 4;i ++) {
if(s[i] == s[i + 1] && s[i + 1] == s[i + 2]) {
res ++;
for(int j = 0;j < i;j ++) t = t + s[j];
for(int j = i + 3;j < 6;j ++) t = t + s[j];
break;
}
}
if(res == 2) {
if(t[0]==t[1]&&t[1]==t[2]) cout << 3 << '\n';
else cout << 2 << '\n';
} else {
map<char,int> mp;
for(int i = 0;i < 6;i ++) mp[s[i]] ++;
for(map<char,int>::iterator it = mp.begin() ;it!=mp.end();it++) {
if((*it).second > 2) {
res ++;
break;
}
}
cout << res << '\n';
}
} else {
int res = 1;
for(int l = 0;l < 9;l ++) {
for(int r = l;r < 9;r ++) {
string tmp1, tmp2;
for(int i = l;i <= r;i ++) tmp1 = tmp1 + s[i];
for(int i = 0;i < l;i ++) tmp2 = tmp2 + s[i];
for(int i = r + 1;i < 9;i ++) tmp2 = tmp2 + s[i];
// cout << tmp1 << " " << tmp2 << endl;
int t1=0,t2=0;
map<char,int> mp1,mp2;
for(int i = 0;i < tmp1.size();i ++) mp1[tmp1[i]] ++;
for(int i = 0;i < tmp2.size();i ++) mp2[tmp2[i]] ++;
for(map<char,int>::iterator it = mp1.begin() ;it!=mp1.end();it++) {
if((*it).second > 2) {
t1 = 1;
break;
}
}
for(map<char,int>::iterator it = mp2.begin() ;it!=mp2.end();it++) {
if((*it).second > 2) {
t2 = 1;
break;
}
}
res = max(res, t1 + t2);
}
}
cout << res << '\n';
}
}
return 0;
}ZOJ 3983简单模拟
C++字符串处理算法
最新推荐文章于 2020-06-19 20:28:35 发布
本文介绍了一种使用C++处理特定字符串模式的问题解决方法。该算法通过检查输入字符串中的重复字符序列来确定需要进行的操作,并根据不同的条件计算出操作数。文章详细展示了如何通过两轮检查来高效地处理字符串,并提供了完整的代码实现。
915

被折叠的 条评论
为什么被折叠?



