본문 바로가기
Computer Science

콩순이의 가장 싼 팰린드롬

by OKOK 2019. 2. 12.

1. DP
2. 문제인가

3. 아직은 해석 하기 쉽지 않음

4. 팰린드롬 찾는 방법

5. 방법을 찾기 


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <stdio.h>
 
#define L_MAX 10
#define K_MAX 26
#define MIN(A, B) ((A) < (B) ? (A) : (B))
 
char buffer[L_MAX + 1];
int L, K;
int cost[K_MAX];
int DT[L_MAX + 1][L_MAX + 1];
 
int main() {
    freopen("input.txt""r", stdin);
    int T; scanf("%d"&T); while (T--) {
        scanf("%d %d"&L, &K);
        scanf("%s", buffer);
 
        for (int i = 0; i < K; i++) {
            int c1, c2; scanf("%d %d"&c1, &c2);
            cost[i] = MIN(c1, c2);
        }
 
        DT[0][L] = 0;
        for (int i = 1; i <= L; i++) {
            DT[i][L] = DT[i - 1][L] + cost[buffer[i - 1- 'a'];
        }
        for (int i = L - 1; i >= 0; i--) {
            DT[0][i] = DT[0][i + 1+ cost[buffer[i] - 'a'];
        }
 
        for (int i = 1; i < L; i++) {
            for (int j = L - 1; j >= i; j--) {
                DT[i][j] = MIN(DT[i - 1][j] + cost[buffer[i - 1- 'a'],
                    DT[i][j + 1+ cost[buffer[j] - 'a']);
                if (buffer[i - 1== buffer[j]) {
                    DT[i][j] = MIN(DT[i][j], DT[i - 1][j + 1]);
                }
            }
        }
 
        int ans = DT[0][1];
        for (int i = 0; i < L; i++) {
            ans = MIN(ans, DT[i][i + 0]);
            ans = MIN(ans, DT[i][i + 1]);
        }
        printf("%d\n", ans);
    }
    return 0;
}
 
cs

 


'Computer Science' 카테고리의 다른 글

러시아 국기 같은 깃발  (0) 2019.02.12
늘어지는 소리 만들기  (0) 2019.02.12
테네스의 특별한 소수  (0) 2019.02.12
콩순이의 팰린드롬  (0) 2019.02.12
다솔이의 다이아몬드 장식  (0) 2019.02.12

댓글