C - Crusaders Quest
题目大意
给你 9 9 9个字母,其中有 3 3 3个 a a a, 3 3 3个 g g g和 3 3 3个 o o o,你每次可以将相同且相邻的 1 1 1~ 3 3 3个字母消去,问你消去连续相同且相邻的三个字母的次数最大是多少
Sample Input
7
gggaaaooo
aaoogggoa
googgaaao
agogaooag
goooggaaa
gogogoaaa
gaogaogao
Sample Output
3
3
2
1
3
2
1
解题思路
考虑消去的字母的先后顺序为 A 3 3 = 6 A_{3}^{3}=6 A33=6种,而字母只有 9 9 9个,直接暴力求解最大的方案即可
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
string s;
int find_pre(string p, char c) {
int len = p.length();
for(int i = 0; i < len; ++i) {
if(p[i] == c) return i;
}
return -1;
}
int find_tail(string p, char c) {
int len = p.length();
for(int i = len - 1; i >= 0; --i) {
if(p[i] == c) return i;
}
return -1;
}
bool find(string p, char c) {
int len = p.length();
int res = 0;
for(int i = len - 1; i >= 0; --i) {
if(p[i] == c) ++res;
}
return res == 3;
}
int gao(char a, char b, char c) {
int res = 0;
string p = s;
int len = p.length();
int x = find_pre(p, a);
int y = find_tail(p, a);
++res;
p = p.substr(0, x) + p.substr(y + 1, len - y - 1);
if(find(p, b)) ++res;
len = p.length();
x = find_pre(p, b);
y = find_tail(p, b);
if(x != -1 && y != -1) {
p = p.substr(0, x) + p.substr(y + 1, len - y - 1);
}
if(p.length() == 3) ++res;
return res;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--) {
cin >> s;