티스토리 뷰

알고리즘/백준(BOJ)

백준 - 10942 팰린드롬?

시나모온 2020. 8. 28. 21:37

문제 링크입니다 : https://www.acmicpc.net/problem/10942

 

10942번: 팰린드롬?

총 M개의 줄에 걸쳐 홍준이의 질문에 대한 명우의 답을 입력으로 주어진 순서에 따라서 출력한다. 팰린드롬인 경우에는 1, 아닌 경우에는 0을 출력한다.

www.acmicpc.net

 

 

 

 

 

 

 

 

#include <iostream>
#include <vector>

using namespace std;

vector<int> seq;
vector<vector<int>> cache;

bool isPalindrome(int a, int b) {
    if(a == b) return true;
    if(a + 1 == b) return seq[a] == seq[b];

    int& ret = cache[a][b];
    if(ret != -1) return ret;

    ret = isPalindrome(a + 1, b - 1) && seq[a] == seq[b];

    return ret;
}

int main() {
    ios_base :: sync_with_stdio(false); 
    cin.tie(NULL); cout.tie(NULL);
    
    int N, M;
    cin >> N;

    cache.assign(N, vector<int>(N, -1));

    seq.resize(N);
    for(int i = 0; i < N; i++) {
        cin >> seq[i];
    }
    
    cin >> M;
    int a, b;
    while(M--) {
        cin >> a >> b;
        cout << isPalindrome(a - 1, b - 1) << '\n';
    }


    return 0;
}

 

 

 

개발 환경 : vscode

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

 

'알고리즘 > 백준(BOJ)' 카테고리의 다른 글

백준 - 1029 그림 교환  (0) 2020.09.24
백준 - 14890 경사로  (0) 2020.09.11
백준 - 1120 문자열  (0) 2020.08.28
백준 - 11656 접미사 배열  (0) 2020.08.28
백준 - 10808 알파벳 개수  (0) 2020.08.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함