九度oj 题目1153:括号匹配问题

题目描述:

    在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注.

输入:

    输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,字符串长度不超过100。
    注意:cin.getline(str,100)最多只能输入99个字符!

输出:

    对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。

样例输入:
)(rttyy())sss)(
样例输出:
)(rttyy())sss)(
?            ?$

开始用两个数组记录

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 #define MAX 102
 7 char temp[MAX];
 8 int left[MAX];
 9 int right[MAX];
10 
11 int main(int argc, char const *argv[])
12 {
13     freopen("input.txt","r",stdin);
14     while(scanf("%s",temp) != EOF) {
15         memset(left,0,sizeof(left));
16         memset(right,0,sizeof(right));
17 
18         int lcnt = 0, rcnt = 0;
19         int len = strlen(temp);
20         for(int i = 0; i < len; i++) {
21             if(temp[i] == '(') {
22                 lcnt++;
23             }
24             else if(temp[i] == ')') {
25                 lcnt--;
26                 if(lcnt < 0) {
27                     left[i] = 1;
28                     lcnt = 0;
29                 }
30             }
31         }
32         for(int i = len-1; i >= 0; i--) {
33             if(temp[i] == ')') {
34                 rcnt++;
35             }
36             else if(temp[i] == '(') {
37                 rcnt--;
38                 if(rcnt < 0) {
39                     right[i] = 1;
40                     rcnt = 0;
41                 }
42             }
43         }
44         puts(temp);
45         for(int i = 0; i < len; i++) {
46             if(left[i] == 1) {
47                 printf("?");
48             }
49             if(right[i] == 1) {
50                 printf("$");
51             }
52             if(left[i] == 0 && right[i] == 0) {
53                 printf(" ");
54             }
55         }
56         puts("");
57     }
58     return 0;
59 }

也可以改成一个

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5  
 6 #define MAX 102
 7 char temp[MAX];
 8 int flag[MAX];
 9  
10 int main(int argc, char const *argv[])
11 {
12     //freopen("input.txt","r",stdin);
13     while(scanf("%s",temp) != EOF) {
14         memset(flag,0,sizeof(flag));
15  
16         int lcnt = 0, rcnt = 0;
17         int len = strlen(temp);
18         for(int i = 0; i < len; i++) {
19             if(temp[i] == '(') {
20                 lcnt++;
21             }
22             else if(temp[i] == ')') {
23                 lcnt--;
24                 if(lcnt < 0) {
25                     flag[i] = 1;
26                     lcnt = 0;
27                 }
28             }
29         }
30         for(int i = len-1; i >= 0; i--) {
31             if(temp[i] == ')') {
32                 rcnt++;
33             }
34             else if(temp[i] == '(') {
35                 rcnt--;
36                 if(rcnt < 0) {
37                     flag[i] = 1;
38                     rcnt = 0;
39                 }
40             }
41         }
42         puts(temp);
43         for(int i = 0; i < len; i++) {
44             if(flag[i] == 1 && temp[i] == ')') {
45                 printf("?");
46             }
47             else if(flag[i] == 1&& temp[i] == '(') {
48                 printf("$");
49             }
50             if(flag[i]== 0) {
51                 printf(" ");
52             }
53         }
54         puts("");
55     }
56     return 0;
57 }

还可以不要这个数组,改为一个char数组

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 #define MAX 102
 7 char temp[MAX];
 8 char temp2[MAX];
 9 
10 int main(int argc, char const *argv[])
11 {
12     freopen("input.txt","r",stdin);
13     while(scanf("%s",temp) != EOF) {
14         puts(temp);
15         int lcnt = 0, rcnt = 0;
16         int len = strlen(temp);
17         temp2[len] = '';
18         for(int i = 0; i < len; i++) {
19             if(temp[i] == '(') {
20                 lcnt++;
21                 temp2[i] = ' ';
22             }
23             else if(temp[i] == ')') {
24                 lcnt--;
25                 if(lcnt < 0) {
26                     temp2[i] = '?';
27                     lcnt = 0;
28                 }
29             }
30             else {
31                 temp2[i] = ' ';
32             }
33         }
34         for(int i = len-1; i >= 0; i--) {
35             if(temp[i] == ')') {
36                 rcnt++;
37                 if(temp2[i] != '?') {
38                     temp2[i] = ' ';
39                 }
40             
41             }
42             else if(temp[i] == '(') {
43                 rcnt--;
44                 if(rcnt < 0) {
45                     temp2[i] = '$';
46                     rcnt = 0;
47                 }
48             }
49             else {
50                 temp2[i] = ' ';
51             }
52         }
53         
54         puts(temp2);
55     }
56     return 0;
57 }
原文地址:https://www.cnblogs.com/jasonJie/p/5714225.html