G. ABBC or BACB

G. ABBC or BACB
贪心
举个例子:BAAAA,BAAAAA,答案是A的总数
注意点:在连续的A中插入B,如:AAABABAA,AAABAA。很明显答案是A的总数减去最小的连续A的个数

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int a[15];
void sovle() {
	string s;
	cin >> s;
	int n=s.length();
	int tot=count(s.begin(),s.end(),'A');//查询A的数量
	if(tot==n||tot==0){//特判
		cout<<"0\n";
		return ;
	}
	if(s[0]=='A'&&s[n-1]=='A'){
		int f=1;
		for(int i=0;i<n-1;i++) {
			if(s[i]=='B'&&s[i+1]=='B'){
				f=0;
				break;
			}
		}
		if(f){
			int last=-1,mn=1e9,sum=0;
			for(int i=0;i<n;i++){
				if(s[i]=='A') sum++;
				else mn=min(mn,sum),sum=0;
			}
			mn=min(mn,sum);
			tot-=mn;
		}
	}
	cout << tot << '\n';
}
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		sovle();
	}
	return 0;
}
posted @   不o凡  阅读(69)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示