【模拟】 【HDU 5831】 Rikka with Parenthesis II

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5831

题解(1011):

http://bestcoder.hdu.edu.cn/blog/2016-multi-university-training-contest-8-solutions-by-%E5%AD%A6%E5%86%9B%E4%B8%AD%E5%AD%A6/

 1 #include<bits/stdc++.h>
 2 
 3 char str[100005];
 4 
 5 int main(){
 6     int T;
 7     scanf("%d", &T);
 8     while(T--){
 9         int n;
10         scanf("%d", &n);
11         scanf("%s", str);
12         if(n&1){
13             puts("No");
14             continue;
15         }
16         int pos = -1;
17         for(int i = 0; i < n; i++){
18             if(str[i] == ')'){
19                 str[i] = '(';
20                 pos = i;
21                 break;
22             }
23         }
24         for(int i = n-1; i >= 0; i--){
25             if(str[i] == '(' && i != pos){
26                 str[i] = ')';
27                 break;
28             }
29         }
30         //for(int i = 0; i < n; i++) printf("%c", str[i]); printf("
");
31         int s = 0;
32         int f = 1;
33         for(int i = 0; i < n; i++){
34             if(str[i] == '(') s++;
35             else{
36                 if(s) s--;
37                 else{
38                     f = 0;
39                     break;
40                 }
41             }
42         }
43         if(!f){
44             puts("No");
45             continue;
46         }
47         if(!s) puts("Yes");
48         else puts("No");
49     }
50 
51     return 0;
52 }
原文地址:https://www.cnblogs.com/miaowTracy/p/5764458.html