WILDCARD 오답 질문드립니다.

  • hyejinJ
    hyejinJ

    종만북을 보고 DP로 작성해보았는데 계속 오답이 뜨네요. 문제에 나온 예제나 ????? *****, 혹은 파일명이 0개인 경우도 테스트해보았는데 다 맞게 나옵니다. 어디가 잘못되었을까요??

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int cache[101][101];
    void sort(char(*list)[51], int N);
    bool solve(char * W, char *word, int x, int y)
    {
        int & ret = cache[x][y];
        if (ret != -1)
            return ret;
    
        int pos = 0;
        while (pos + x < strlen(W) && pos + y < strlen(word) &&
            (W[pos + x] == '?' || W[pos + x] == word[pos + y])) {
            pos++;
        }
        if (pos + x == strlen(W))
            return ((pos + y) == strlen(word));
    
        if (W[pos + x] == '*')
        {
            for (int i = pos; y + i <= strlen(word); i++)
            {
                if (solve(W, word, x + pos + 1, y + i) == true)
                    return ret = 1;
            }
            return ret = 0;
        }
        return ret = false;
    }
    
    int main(void)
    {
        int C;
        cin >> C;
        while (C--)
        {
            char W[101];
            char list[101][51];
            int N;
    
            cin >> W;   //pattern
            cin >> N;   //파일 수
            int cnt = 0;
            for (int i = 0; i < N; i++)
            {
                memset(cache, -1, sizeof(cache));
                cin >> list[cnt];
                if (solve(W, list[cnt], 0, 0))
                    cnt++;
            }
            if (cnt > 0)
                sort(list, cnt);
    
            for (int i = 0; i < cnt; i++)
                cout << list[i] << endl;
        }
        return 0;
    }
    
    void sort(char(*list)[51], int N)   //merge sort
    {
        if (N == 1)
            return;
    
        int half = N / 2;
        sort(list, half);
        sort(&list[half], N - half);
    
        char temp[101][51];
        int lidx = 0;
        int ridx = half;
        for (int i = 0; i < N; i++)
        {
            if (lidx == half)
                strcpy(temp[i], list[ridx++]);
    
            else if (ridx == N)
                strcpy(temp[i], list[lidx++]);
    
            else if (strcmp(list[lidx], list[ridx]) < 0)
                strcpy(temp[i], list[lidx++]);
    
            else
                strcpy(temp[i], list[ridx++]);      
        }
        for (int i = 0; i < N; i++)
            strcpy(list[i], temp[i]);
    }
    

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