牛客小白月赛87

文章描述了一个编程挑战,涉及在C++代码中处理括号删除操作,解决输入字符串中I标识的括号对的问题。作者给出了一个解决方法,用于处理delete操作和保持括号平衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C-小苯的IDE括号问题(easy)_牛客小白月赛87 (nowcoder.com) 


#include <iostream>
#include <algorithm>
#include <cstring>

#define x first
#define y second
using namespace std;
using LL = long long;

typedef pair<int, int> PII;

const int N = 200010;

int n, m;
char s[N];
void solve()
{
	int id = 0;
	cin >> n >> m;
	cin >> s + 1;
	for (int i = 1; i <= n; i++)
		if (s[i] == 'I')
		{
			id = i; break;
		}
	int l = id - 1, r = id + 1;
	string op;
	while (m--)
	{
		cin >> op;
		if (op == "delete")
		{
			if (r <= n )
			{
				s[r] = 0;
				r++;
			}
		}
		else
		{
            if (l == 0) continue;
			if (r <= n && s[l] == '(' && s[r] == ')')
			{
				s[l] = 0, s[r] = 0;
				l--; r++;
			}
			else
			{
					s[l] = 0;
					l--;
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (s[i] != 0) cout << s[i];
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值