2017ccpc秦皇岛解题报告

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;
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值