统计a字符右边是否有足够多的b字符与之匹配

文章描述了如何解决Codeforces中的一个问题,通过遍历输入字符串并统计字符a的数量,同时处理其他字符的情况。程序还需检查字符串首字符是否为b,以及a和y计数是否相等。
摘要由CSDN通过智能技术生成
for (char c : s) {
    if (c == 'a') {
        cnt++;
    } else {
        cnt = max(cnt - 1, 0ll);
    }
}

23ccpc桂林GProblem - G - Codeforces

void solve() {
	std::string s;
	std::cin >> s;
	int cnt = 0;
	for (char c : s) {
		if (c == '(') {
			cnt++;
		}
		else {
			cnt = std::max(cnt - 1, 0);
		}
	}
	std::cout << (cnt ? "impossible" : s) << '\n';
}

signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0), std::cout.tie(0);
	int t; 
	std::cin >> t; 
	while (t--) {
		solve();
	}
	return 0;
}

好串登录—专业IT笔试面试备考平台_牛客网

using ll=long long;
void solve() {
	std::string s;
	std::cin >> s;
	ll cnt = 0,x=0,y=0;
	for (char c : s) {
		if (c == 'a') {
			cnt++;
            x++;
		}
		else {
			cnt = std::max(cnt - 1LL, 0LL);
            y++;
		}
	}
    if(s[0]=='b'||x!=y)cnt=1LL;
	std::cout << (cnt ? "Bad" : "Good") << '\n';
}

signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0), std::cout.tie(0);
	int t=1; 
	while (t--) {
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值