C. ABBB

13 篇文章 0 订阅

题目:
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters ‘A’ and ‘B’. He can use bombs to bomb a substring which is either “AB” or “BB”. When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.

For example, Zookeeper can use two such operations: AABABBA → AABBA → AAA.

Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?

https://codeforces.com/contest/1428/problem/C

题意:
给出一个字符串,相邻的AB可以消掉,相邻的BB可以消掉,问最少可以剩下多少个字母。

思路:
直接字符串模拟就行了。不过应该优先消除AB,再消除BB。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 200020;
int t;
long long int num = 0;
int vis[MAXN];
int b[MAXN];
int main() 
{
	int t;
	cin >> t;
	while (t--) 
	{
		string s;
		cin >> s;
		for (int i = 0; i < s.size() - 1 && s.size(); i++) 
		{
			if (s[i] == 'A' && s[i + 1] == 'B') 
			{
				s.erase(s.begin() + i, s.begin() + i + 2);
				i -= 2;
				if (i == -2)
					i += 1;
			}
			else if (s[i] == 'B' && s[i + 1] == 'B') 
			{
				s.erase(s.begin() + i, s.begin() + i + 2);
				i -= 2;
				if (i == -2)
					i += 1;
			}
		}
		if (t > 0)
			cout << s.size() << '\n';
		else
			cout << s.size();
	}
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值