FIXPAREN에 관한 질문

  • pyeonbun
    pyeonbun
    package hello;
    
    import java.util.Arrays;
    import java.util.Scanner;
    import java.util.Stack;
    
    public class PatternMatch {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
    
            int repeat = 0;
    
            Stack<Character> open = new Stack<>();
    
            repeat = input.nextInt();
    
            input.skip("[\\r\\n]+");  
    
            while(repeat --> 0){
                open.clear();
    
                String[] buf = input.nextLine().split(" ");
                char[] data = buf[0].toCharArray();
                char[] priority = buf[1].toCharArray();
                char[] result = new char[data.length];
    
                int stackCount = 0;
    
                for(int i=0,n=data.length;i<n;i++){
                    if(openTag(data[i])){   // openTag일 때
                        result[i] = (data[i]);
                        open.push(data[i]);
                        stackCount = i;
                    }else if(closeTag(open.peek(),data[i])){ // closeTag면서 매치가 되었을 때
                        result[i] = (data[i]);
                        open.pop();
                        stackCount -= 1;
                    }else{  //closeTag 매치가 안되었을 떄
                        stackCount = matchTag(result, priority, open.peek(), data[i], stackCount,i);
                        open.pop();
                    }
                }
    
                for(int i=0;i<result.length;i++){
                    System.out.print(result[i]);
                }
                System.out.println();
    
            }// while
        }// main
    
    
        public static boolean openTag(char ch){
             if(ch == '(' || ch == '{' || ch == '[' ||ch == '<'){
                 return true;
             }
    
             return false;
        }
    
        public static boolean closeTag(Character open,char ch){
            switch(ch){
            case ')':
                return open.equals('(');
            case '}':
                return open.equals('{');
            case ']':
                return open.equals('[');
            case '>':
                return open.equals('<');
            default :
                return false;
            }    
        }
    
        public static char reverseTag(char ch,boolean plus){
            if(plus == true){
                if(ch == '('){
                    ch += 1;
                    return ch;
                }
    
                ch += 2;
    
            }else if(plus == false){
                if(ch == ')'){
                    ch -= 1;
                    return ch;
                }
    
                ch -= 2;
            }
    
            return ch;
        }
    
        public static int matchTag(char[] result,char[] priority,Character open,Character ch,int openLocation,int closeLocation){
            Character reverseChar = reverseTag(ch, false);
            Character reverseOpen = reverseTag(open, true);
    
            int openPriority = 0;
            int chPriority = 0;
    
            for(int i=0;i<4;i++){   // 0이 가장 우선순위가 높음
                if(open.equals(priority[i])){
                    openPriority = i;
                }
    
                if(reverseChar.equals(priority[i])){
                    chPriority = i;
                }
            }
    
            if(openPriority > chPriority){  //ch의 우선순위가 높을 때  
                result[openLocation] = reverseChar;
                result[closeLocation] = ch;
            }else if(openPriority < chPriority){    //open의 우선순위가 높을 때
                result[closeLocation] = reverseOpen;
            }
    
            return openLocation-1;
        }
    
    
    }// class
    

    자주 글을 올려서 정말 죄송합니다.

    문제 주소 : FIXPAREN

    문제이해
    오픈태그일 때는 저장하고 닫는 태그일 때 오픈태그랑 비교해서 우선순위를 확인해 변경하는걸로 문제를 이해했습니다

    메소드 설명
    openTag는 오픈 태그일 때 true 를 반환하고 아니면 false를 반환 합니다

    closeTag는 닫는 태그일 때 오픈 태그랑 일치한다면 true 를 반환하고 아니면 false를 반환 합니다

    matchTag는 닫는 태그가 오픈 태그랑 일치하지 않을 때 오픈태그랑 우선순위를 확인해 변경하는 로직입니다

    reverseTag는 닫는 태그를 오픈 태그로 변환시켜서 반환하거나 그 반대에 경우로 반환에 줍니다

    질문사항

    1. }( <({[ 케이스일 때도 ()이렇게 정상 작동해야 하나요?
    2. 첫번째 질문이 아니라면 테스트케이스나 조언을 해주시면 감사히 받겠습니다.

    7년 전
2개의 댓글이 있습니다.
  • hyunhwan
    hyunhwan

    글 자주 올리시는 것에 대해서는 죄송할 필요가 없습니다!

    1. 문제를 확인해보면 다음과 같은 경우가 들어오지 않는다는 가정이 있습니다.
    2. 간단하게 테스트를 해봤는데, 올바르게 구성되지 않은 출력이 나옴을 확인할 수 있었습니다. 이 부분 확인 해보시길 바랍니다.

    7년 전 link
  • pyeonbun
    pyeonbun

    hyunhwan님 감사합니다


    7년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.