#10962: java,測資點1過不了...請大家幫忙看看哪裡出了錯


landy6424 (landy)

學校 : 國立中興大學
編號 : 58025
來源 : [220.132.6.235]
最後登入時間 :
2016-06-14 20:49:39
b304. 00673 - Parentheses Balance -- UVa673 | From: [220.132.6.235] | 發表日期 : 2016-05-29 13:09

import java.util.*;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.print("");
        Scanner keybroad=new Scanner(System.in);
        String cs=keybroad.nextLine();  //case
        String [] Can;
        Can=new String [128];  //字串大小限128
        int [] AS;                    //建立兩陣列
        AS=new int[Integer.parseInt(cs)];
        int [] BS;
        BS=new int[Integer.parseInt(cs)];
        int Re=0;
        java.util.Arrays.fill(Can,"");    //將Can用空字串填滿
        for(int i=0;i<Integer.parseInt(cs);i++){
            String test=keybroad.nextLine();
            int len=test.length();
            Re=len;     //Re=字串長度
            BS[i]=Re;
            Can=test.split("");
            int j=0;
            int sum=0;
            Determine(Can,j,len,Re);
            for(int w=0;w<Re;w++){   //若有非空字串存在則表運算式不正確
                if(Can[w].equals("")){
                    sum=sum+1;
                }
                else{
                    Can[w]="";         //清掉以供下一個case使用
                }
            }
            AS[i]=sum;            
            sum=0;            
            Re=0;
        }
        for(int s=0;s<Integer.parseInt(cs);s++){
            if(AS[s]==BS[s]){  
                System.out.println("Yes");
            }
            else{                
                System.out.println("No");
            }
        }
}
    public static void Determine(String array[],int j,int len,int Re){
        while(len!=0){
            if(j>=Re){
                len=0;
                Determine(array,j,len,Re);
            }
        else if(array[j].equals("(")){
            Correct1(array,j,len,Re);   //判斷有無右小括弧
        }
        else if(array[j].equals("[")){  //判斷有無右中括弧
            Correct2(array,j,len,Re);
        }
        else if((array[j].equals(")"))||(array[j].equals("]"))){
            if((j-1)>=0){  //往前探索有無左括弧
                j=j-1;
                Determine(array,j,len,Re);
            }
            else{  //無則跳出Determine
                len=0;
                Determine(array,j,len,Re);
            }
        }
        else{   //跳出Determine
            len=0;
            Determine(array,j,len,Re);
        }        
        }                
    }
    public static void Correct1(String array[],int j,int len,int Re){
            if(array[j+1].equals(")")){
                array[j]="";        //清掉
                array[j+1]="";
                for(int r=j+2;r<len;r++){   //所有字元向前移動兩格
                    array[r-2]=array[r];
                    array[r]="";
                }
                len=len-2;    
                Determine(array,j,len,Re);
            }            
            else{    //往後
                j=j+1;
                Determine(array,j,len,Re);
            }
        }

    public static void Correct2(String array[],int j,int len,int Re){
            if(array[j+1].equals("]")){
                array[j]="";           //清掉
                array[j+1]="";
                for(int r=j+2;r<len;r++){   //所有字元向前移動兩格
                    array[r-2]=array[r];
                    array[r]="";
                }
                len=len-2;
                Determine(array,j,len,Re);
            }
                    
            else{   //往後
            j=j+1;
            Determine(array,j,len,Re);
        }
    }
}

 
ZeroJudge Forum