WILDCARD 질문

  • User Avatar
    bjkim07

    [[problem:WILDCARD]] 를 지금 풀고 있습니다.

    책에 있는 예제로는 맞게 나오고 다른 예제를 넣어봐도 어느 정도 맞는 것 같은데 크리티컬 한 예제가 어떤 것이 있을까요?

    알고리즘은 참 재밌긴 한데 어렵네요 @.@;;;;


    ~~~ java
    import java.io.*;
    import java.util.*;

    public class Main {
    private int n_case;
    private String wild;
    private String pb;
    private ArrayList res = new ArrayList ();

    public static void main(String[] args) {
        Main test = new Main();
        test.in();
        test.out();
    }
    public void out() {
        Collections.sort(this.res);
        for (String a:this.res) 
            System.out.println(a);
    }
    public void in () {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try {
            this.n_case = Integer.parseInt(br.readLine());
            for (int i=0; i<this.n_case; i++) {
                this.wild = br.readLine();
                int s_word = Integer.parseInt(br.readLine());
                for (int j=0; j<s_word; j++) {
                    this.pb = br.readLine();
                    if (sol(this.wild, this.pb)) {
                        this.res.add(this.pb);
                    }
                }
            }
        } catch (Exception ex) {System.out.println (ex);}
    }
    
    public boolean sol (String wild, String name) {
        while ( (wild.length()>0) && (name.length()>0) && (wild.charAt(0) == name.charAt(0) || wild.charAt(0) == '?')) {
            wild = wild.substring(1);
            name = name.substring(1);
        } 
    
        if (wild.isEmpty())
            return wild.isEmpty() && name.isEmpty();
    
        if (wild.charAt(0) =='*' && wild.length() == 1) 
            return true;
    
        if (wild.charAt(0) == '*' ) {
            wild = wild.substring(1);
            for (int i=0; i < name.length() ; i++) {
                String next_name = name.substring(i);
                if (sol (wild, next_name)) 
                    return true;
            }
        }
        return false;
    }
    

    }
    ~~~


    13년 전
6개의 댓글이 있습니다.
  • User Avatar
    Being

    글쓰기 도움말을 살펴 보시고 코드를 구문 강조 되도록 표시해 주시면 다른 분들이 답변 주시기에 도움이 될 것 같습니다.


    13년 전 link
  • User Avatar
    bjkim07

    앗 그렇군요. 감사합니다. Being 님.


    13년 전 link
  • User Avatar
    Kureyo

    전체답 소트가 아니라 테스트 케이스별로 답이 소트되어야합니다 @_@


    13년 전 link
  • User Avatar
    bjkim07

    억...Kureyo 님... 그런 것이군요!!! 감사합니다!!!!


    13년 전 link
  • User Avatar
    bjkim07

    음 일단 뭔가 전체적인 문제가 있네요.. 다시 한번 풀어보고 질문 드릴께요~ 도움주셔서 감사합니다. Being 님 Kureyo 님 :D


    13년 전 link
  • User Avatar
    bjkim07

    휴 해결했습니다. 알고리즘 적으로는 *가 마지막에 연달아 나오고 비교할 word 가 남아 있지 않을 때 문제가 발생하는 것이었네요! Kureyo님의 케이스별로 답이 소트되어야 한다는 것이 가장 크리티컬한 도움이었습니다!!! 오늘 여러번 도움을 받네요. 감사합니다. :D


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