12.最大UCC子串【贪心算法已解决】

#include <iostream>
#include <vector>
#include <string>

//首先贪心算法,明确插入是最好策略
//将问题转化为寻找字串ucc  uc cu cc uu 这些的花费
int solution(int m, std::string s) {
    // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
    // write code here
    //寻找UCC 无代价 UC CC  一代价  子串 
    std::vector<int> consume = {0,0,0}; //记录代价的数量。 eg:consume[0]的值表示UUC子串数量
    std::string temp_string = "";
    for(int i = 0; i < s.size(); i++){
        if(temp_string == "UC"){
            if(s[i] == 'C'){
                consume[0]++;
                temp_string = ""; //记录清零
            }else if(s[i] == 'U'){
                consume[1]++;
                temp_string = "U";
            }
        }else if(temp_string == "C"){
            if(s[i] == 'C'){
                consume[1]++;
                temp_string = ""; //记录清零
            }else if(s[i] == 'U'){
                consume[2]++;
                temp_string = "U"; //重新记录
            }
        }else if(temp_string == "U"){
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值