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
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
C - Move Brackets | GNU C++17 | Accepted | 30 ms | 3600 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;
}