[codeforces 1374C] Move Brackets 括号()配对

Codeforces Round #653 (Div. 3)   参与排名人数11687

[codeforces 1374C]    Move Brackets    括号()配对

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址http://codeforces.com/contest/1374/problem/C

ProblemLangVerdictTimeMemory
C - Move Brackets GNU C++17Accepted30 ms3600 KB

样例模拟如下:

2
)(

1
位置12
括号)(
l代表未配对的(数量
r代表未配对的)数量

扫描到位置1:l=0,r=1
扫描到位置2:l=1,r=1
需移动1次


4
()()

0
位置1234
括号()()
l代表未配对的(数量
r代表未配对的)数量

扫描到位置1:l=1,r=0
扫描到位置2:l=0,r=0
扫描到位置3:l=1,r=0
扫描到位置4:l=0,r=0
需移动0次


8
())()()(

1
位置12345678
括号())()()(
l代表未配对的(数量
r代表未配对的)数量

扫描到位置1:l=1,r=0
扫描到位置2:l=0,r=0
扫描到位置3:l=0,r=1
扫描到位置4:l=1,r=1
扫描到位置5:l=0,r=1
扫描到位置6:l=1,r=1
扫描到位置7:l=0,r=1
扫描到位置8:l=1,r=1
需移动1次

10
)))((((())

3
位置12345678910
括号)))((((())
l代表未配对的(数量
r代表未配对的)数量

扫描到位置1:l=0,r=1
扫描到位置2:l=0,r=2
扫描到位置3:l=0,r=3
扫描到位置4:l=1,r=3
扫描到位置5:l=2,r=3
扫描到位置6:l=3,r=3
扫描到位置7:l=4,r=3
扫描到位置8:l=5,r=3
扫描到位置9:l=4,r=3
扫描到位置10:l=3,r=3
需移动3次

AC代码如下:

#include <stdio.h>
char s[55];
int main(){
	int t,n,i,l,r;
	scanf("%d",&t);
	while(t--){
		scanf("%d%s",&n,s);
		l=r=0;
		for(i=0;i<n;i++)
			if(s[i]=='(')l++;
			else if(s[i]==')'){//s[i]==')'
				if(l)l--;//()配对
				else r++;
			}
		printf("%d\n",r);//余下未配对的)数量
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值