acm练习-day1

描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class Main {
    public static String jud(char[] a){
        int result = 1;
        if (a.length%2!=0) {
            result=0;
        }
        else {
            int qkh =0,hkh = 0;
            for (int i = 0; i < a.length; i++) {
                if (qkh<hkh||(qkh==hkh&&(a[i]==']'||a[i]==')'))) {
                    result = 0;
                }
                else {
                     
                    if (a[i]=='['||a[i]=='(') {
                        qkh++;
                    }
                    else if(a[i]==']'||a[i]==')'){
                        hkh++;
                    }
                    if ((a[i] == '['&&a[i+1]==')')||(a[i] == '('&&a[i+1]==']')) {
                        result = 0;
                    }
                }
                     
                }
                 
                 
            }
        if (result == 1) {
            return "Yes";
        }
        else {
            return "No";
        }
        }
         
     
public static void main(String[] args) throws Exception {
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(buf.readLine());
        String r ="";
        for (int i = 0; i < a; i++) {
            if (r!="") {
                r = r+"@"+jud(buf.readLine().toCharArray());
            }
            else {
                r = jud(buf.readLine().toCharArray());
            }
        }
        String[] k = r.split("@");
        for (int i = 0; i < k.length; i++) {
            System.out.println(k[i]);
        }
         
}
}

  

posted @   孙秋翔_quinns  阅读(224)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示