hdu5-Touhou Red Red Blue(贪心)

Problem - 7329 (hdu.edu.cn)

参考:题解 | #1006.Touhou Red Red Blue# 2023杭电暑期多校5 

题解:(贪心)

mp['R'], mp['G'], mp['P'] 分别记录对应字母出现过多少次,没有'AAA' or'ABC' 出现时不得分也不进行任何操作,有 'ABC' 出现则扔掉三个字母并自定义两个字母,自定义的两个字母和下一个给定字母组成 'AAA' ,或 mp['R']&&mp['G']&&mp['P'] 每种字母均出现时形成 'AAA',后扔掉 'AAA' 并自定义一个字母,接下来只有两种情况,即 '_AA' or '_AB' 。

注意 i 值的传递

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod=998244353;
const int inf=1<<30;
const double eps=1e-7;
int T,res,l;
string s;
map<char,int>mp;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//inline int ABC(int i);
inline int AAA(int i){
	res++;
	while(i+1<l){
		if(s[i]==s[i+1])res++,i+=2;
		else {
			if(i+2<l)res++;
			i+=3;
		}//return ABC(i+3);
	}return i+1;
}
//inline int ABC(int i){
//	if(i<l)return AAA(i+1);
//}

int main(){
	T=read();
    while(T--){
    	cin>>s;
    	l=s.length();
    	res=0;
		mp['R']=mp['G']=mp['B']=0;
		//mp[s[0]]++;mp[s[1]]++;
    	for(int i=0;i<l;i++){
    		mp[s[i]]++;
    		//printf("start s[%d]%c res:%d R:%d G:%d B:%d\n",i,s[i],res,mp['R'],mp['G'],mp['B']);
    		if(mp['R']&&mp['G']&&mp['B']){//ABC
    			if(i+1<l)i=AAA(i+2);
				//i=ABC(i+1);
    			//mp['R']=mp['G']=mp['B']=0;
				//mp[s[i-2]]++;mp[s[i-1]]++;
			}else if(mp['R']==3||mp['G']==3||mp['B']==3){//AAA
				i=AAA(i+1);
				//mp['R']=mp['G']=mp['B']=0;
				//mp[s[i-2]]++;mp[s[i-1]]++;				
			}/*else{//AAB
				i++;
			}//printf("s[%d]%c res:%d R:%d G:%d B:%d\n",i,s[i],res,mp['R'],mp['G'],mp['B']);
			*/
		}printf("%d\n",res);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值