牛客 彩色瓷砖

###题意:
给一个字符串每个字符可以是’R’, ‘G’, ‘B’或者’Y’,让字符中没有相邻的两个字符相同,要对原字符串最少的修改次数。
###思路:
相邻且相同的子串长度为len,len为奇数,则只需修改第奇数个的位置,所以修改的数量是len/2(整除),len为偶数,也只修改偶数个位置,即可满足情况。
###代码:

#include <bits/stdc++.h>
#define pi acos(-1)
#define pb push_back
#define LL long long
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define local freopen("in.txt","r",stdin)
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 0x7FFFFFFF;
const int MAX = 1000000;
inline void read(int& x){
    int flag = 1; char c; while(((c = getchar()) < '0' || c > '9') && c != '-');
    c == '-' ? (flag = -1, x = 0) : (x = c - '0');
    while((c = getchar()) >= '0' && c <= '9') { x = x * 10 + c - '0'; } x *= flag;
}
char str[20];
int main(){
    int len, i, cur_continue_num = 1, ans = 0;
    scanf("%s", str);
    len = strlen(str);
    for(i = 1; i < len; ++i){
        if(str[i] == str[i - 1]){
            ++cur_continue_num;
        }
        else{
            ans += cur_continue_num / 2;
            cur_continue_num = 1;
        }
    }
    ans += cur_continue_num / 2;
    printf("%d\n", ans);
    return 0;
}

###总结:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值