CodeForces - 1323C C - Unusual Competitions

22 篇文章 0 订阅

题目:https://codeforces.com/problemset/problem/1323/C

题解:
有一个特殊的点位是“)(”类型的,虽然两个数量都是1,但是顺序不正确,加上这个就应该能过了

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))

template <typename T>
void read(T &x) {
	int s = 0, c = getchar();
	x = 0;
	while (isspace(c))
		c = getchar();
	if (c == 45)
		s = 1, c = getchar();
	while (isdigit(c))
		x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	if (s)
		x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
	int b[40], l = 0;
	if (x < 0)
		putchar(45), x = -x;
	while (x > 0)
		b[l++] = x % 10, x /= 10;
	if (!l)
		putchar(48);
	while (l)
		putchar(b[--l] | 48);
	putchar(c);
}

int main(void) {
//	int kase;
//	read(kase);
//	for (int ii = 1; ii <= kase; ii++) {
		int n;
		read(n);
		//getchar();
		char s[1000010];
		scanf("%s",&s);

		
		int ans=0,sum=0;
		for(int i=0;i<n;i++){
			if(s[i]=='(')ans++;else ans--;
			if(ans<0||ans==0&&s[i]=='(') sum++;
		}
		
		printf("%d\n", ans != 0 ? -1 : sum);
//	}
	return 0;
}

参考:https://xiaoxiaoh.blog.csdn.net/article/details/104749170

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值