알고리즘 문제 해결 전략문제 중 - LIS

  • Jaekwan
    Jaekwan

    알고리즘 문제해결 전략 잘 보고 있습니다.

    오늘 LIS 부분을 해결하던 중 p.233에 동적계획법을 사용해 해법을 조금더 빠르게 바꾸는 부분이 있는데, 이 해법에 오류가 있는지 여쭈어 보고 싶습니다.
    cache를 꺼내는 부분만 있고 cache에 집어넣는 부분이 누락된 것 같아 말씀드립니다.

    그리고

    아래와 같이 소스를 만들어 보았는데 제출하면 오류가 나더군요. 본문에 있는 테스트 케이스는 잘 통과하고 이것 저것 혼자 만들어본 테스트 케이스도 잘 통과하는데 오류가 나서 도움을 구해 봅니다.

    #include <cstring>
    #include <string.h>
    #include <map>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <complex>
    #include <list>
    #include <unistd.h>
    
    using namespace std;
    #define MAX 501
    int N;
    int cache[MAX];
    int array[MAX];
    
    
    int readIntsToArray(string &s, int* array) {
    
        stringstream ss(s);
        int temp;
        int n = 0;
    
        while(ss>>temp) {
            array[n++] = temp;
        }
    
        return n;
    }
    
    int getMax(int start, int n) {
    
        if (cache[start] != -1) {
            return cache[start];
        }
    
        int ret = 1;
        for (int next = start +1 ; next < n; next++) {
            if (array[start] < array[next]) {
                ret += getMax(next, n);
                cache[start] = ret;
                break;
            }
        }
    
        return ret;
    }
    
    int solve(int n, int *array) {
        int maxLen = -1;
    
        for(int i = 0 ; i<n; i++){
            maxLen = max(getMax(i, n), maxLen);
        }
    
        return maxLen;
    }
    
    int main() {
    
        int el_len;
        string s;
        cin>>N;
        while(N--) {
    
            vector<int> v1;
    
            cin>>el_len;
            getline(cin,s);             // after cin, null line comes...
            getline(cin,s);
    
            // clear memory
            memset(cache, -1, sizeof(cache));
            memset(array, -1, sizeof(array));
    
            int n = readIntsToArray(s, array);
    
    
            cout<<solve(n, array)<<endl;
        }
        return 0;
    }
    


    9년 전
3개의 댓글이 있습니다.
  • JongMan
    JongMan

    우선 첫번째 질문만 답변드리자면.. ret는 레퍼런스 타입입니다. ret의 값이 변하면 ret가 가리키는 cache내의 값도 변하죠.


    9년 전 link
  • Jaekwan
    Jaekwan

    아! 레퍼런스 타입! 죄송합니다. c++로 넘어 온지 얼마 안되서 레퍼런스 타입에 대한 이해가 부족했군요!. 한 가지 배우고 갑니다!


    9년 전 link
  • Jaekwan
    Jaekwan

    수열이 오름 차순이라고 잘못 생각하고 있었군요. 오류 확인 했습니다.


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