BOJ : https://www.acmicpc.net/problem/2011

github : https://github.com/junho0956/Algorithm/blob/master/2011/2011/%EC%86%8C%EC%8A%A4.cpp

 

경우의 수를 되짚어보겠습니다.

 

1) 현재 수가 0 초과일때 => 무조건 한자리 수는 만들 수 있습니다.

이게 의미하는 것은 결국 가짓수에 변화가 없다는 뜻입니다.

ex ) ...23을 읽고 있을 때 2와 3을 기준 한자리 수가 의미하는 것은 BC 를 만드는 것이라는 뜻입니다.

 

2) 현재 수와 직전의 수를 합했을 때의 범위를 봅시다

현재수 + 직전의수*10 이 만약 10이상 26이하라면

현재수-2까지 만들수 있는 가짓수에다가 새로운 가짓수가 생긴 것입니다.

ex ) ...323을 읽고 있을 때 그 수는 23이 되고, 이의 경우 CB3 => CBC 또는 C에 새로운가짓수 23인 W가 되어 CW 

 

이렇게까지만해서 제출했더니 WA를 받았습니다.

게시판을 보니 앞자리에 0이 나올 수 있다고 하더라구요... 그래서 그 부분만 처리해줬더니 AC를 받았습니다

 

dp[0] = dp[1] = 1 을 해준 이유는,

직접 해보시면 알겠지만 최소한 맨앞이 0이 아니라면

무슨 숫자든 1가지의 경우는 만들 수 있다는 뜻입니다.

그리고 저는 for문을 string의 2번째인자부터 읽었기 때문입니다. 그래서 str[i-1]을 사용한 것이구요

그렇기때문에 첫 인자인 1을 읽을 때 else에 대해 범위를 벗어나지 않기위해 2개를 동시에 1이라고 초기화한 것 입니다.

 

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
#include <iostream>
#include <string>
using namespace std;
#define MOD 1000000
 
int dp[5001];
string str;
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> str;
 
    if (str[0== '0') {
        cout << "0";
        return 0;
    }
    else {
        dp[0= 1;
        dp[1= 1;
        for (int i = 2; i <= str.length(); i++) {
            if (str[i-1- '0' > 0) {
                dp[i] = dp[i - 1] % MOD;
            }
            int num = (str[i-1- '0'+ (str[i - 2- '0'* 10;
            if (num > 9 && num < 27) {
                dp[i] = (dp[i] + dp[i - 2]) % MOD;
            }
        }
 
        cout << dp[str.length()];
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1890번 점프  (0) 2020.02.13
BOJ 9184번 신나는 함수 실행  (0) 2020.02.13
BOJ 2638번 치즈  (0) 2020.02.13
BOJ 2636번 치즈  (0) 2020.02.13
BOJ 13711번 LCS 4  (0) 2020.02.12

BOJ : https://www.acmicpc.net/problem/2638

github : https://github.com/junho0956/Algorithm/blob/master/2638/2638/%EC%86%8C%EC%8A%A4.cpp

 

2636번 치즈에서 조건이 2개의 공기와 마주친다는 점만 다릅니다.

이외에는 구현이 비슷합니다.

2개의 공기와 마주치는 점들을 따로 큐에 모아놓고, 그 점들을 삭제하면서 구멍과 마주치는 부분이 있는지

확인하였습니다.

 

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <queue>
using namespace std;
 
typedef pair<intint> pii;
int arr[101][101];
int n, m;
bool visit[101][101];
int my[4= { -1,1,0,0 };
int mx[4= { 0,0,1,-1 };
queue<pair<pii, int> > q;
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> arr[i][j];
            if (arr[i][j]) q.push({ {i,j},0 });
        }
    }
 
    queue<pair<intint> > out;
    bool check = false;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (arr[i][j] == 0) {
                check = true;
                out.push({ i,j });
                while (!out.empty()) {
                    int y = out.front().first;
                    int x = out.front().second;
                    out.pop();
                    if (visit[y][x]) continue;
                    visit[y][x] = true;
                    arr[y][x] = 2;
                    for (int i = 0; i < 4; i++) {
                        int yy = y + my[i];
                        int xx = x + mx[i];
                        if (yy >= 0 && yy < n && xx >= 0 && xx < m && !visit[yy][xx] && !arr[yy][xx]) {
                            out.push({ yy,xx });
                        }
                    }
                }
                break;
            }
        }
        if (check) break;
    }
 
    queue<pii> hole;
    int time = 0;
    while (1) {
        int del = 0;
        while (!q.empty()) {
            int y = q.front().first.first;
            int x = q.front().first.second;
            int cnt = q.front().second;
            if (time != cnt) break;
            q.pop();
            int out_cnt = 0;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (yy >= 0 && yy < n && xx >= 0 && xx < m && arr[yy][xx] == 2) {
                    out_cnt++;
                }
            }
            if (out_cnt >= 2hole.push({ y,x });
            else q.push({ {y,x},cnt + 1 });
        }
 
        while (!hole.empty()) {
            int y = hole.front().first;
            int x = hole.front().second;
            hole.pop();
            arr[y][x] = 2;
            del++;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (yy >= 0 && yy < n && xx >= 0 && xx < m && !arr[yy][xx]) out.push({ yy,xx });
            }
        }
 
        while (!out.empty()) {
            int y = out.front().first;
            int x = out.front().second;
            out.pop();
            if (visit[y][x]) continue;
            visit[y][x] = true;
            arr[y][x] = 2;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (yy >= 0 && yy < n && xx >= 0 && xx < m && !arr[yy][xx]) out.push({ yy,xx });
            }
        }
 
        if (!del) break;
        time++;
    }
 
    cout << time;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 9184번 신나는 함수 실행  (0) 2020.02.13
BOJ 2011번 암호코드  (0) 2020.02.13
BOJ 2636번 치즈  (0) 2020.02.13
BOJ 13711번 LCS 4  (0) 2020.02.12
BOJ 1958번 LCS 3  (0) 2020.02.12

BOJ : https://www.acmicpc.net/problem/2636

github : https://github.com/junho0956/Algorithm/blob/master/2636/2636/%EC%86%8C%EC%8A%A4.cpp

 

제가 구현 실력이 부족한거겠지만

정올 초등부(3번) 수준이 이정도라니 정말 놀랐습니다ㅠㅠ

중간에 구멍이 뚫리면 공기와 만난다는 점만 고려해주셔서 구현해주시면 될 것 같습니다.

 

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <queue>
using namespace std;
 
typedef pair<intint> pii;
 
queue<pair<pii, int> > q;
queue<pii> delq;
int arr[101][101];
bool visit[101][101];
int my[4= { -1,1,0,0 };
int mx[4= { 0,0,1,-1 };
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> arr[i][j];
            if (arr[i][j]) q.push({ {i,j},0 });
        }
    }
 
    queue<pii> hole;
    hole.push(0,0 });
    while (!hole.empty()) {
        int y = hole.front().first;
        int x = hole.front().second;
        hole.pop();
 
        if (visit[y][x]) continue;
        visit[y][x] = true;
        arr[y][x] = 2;
 
        for (int i = 0; i < 4; i++) {
            int yy = y + my[i];
            int xx = x + mx[i];
            if (yy >= 0 && yy < n && xx >= 0 && xx < m && !visit[yy][xx] && !arr[yy][xx]) {
                hole.push({ yy,xx });
            }
        }
    }
 
    int time = 0;
    int ans;
    while (1) {
        bool finish = true;
        int cnt = 0;
 
        while (!q.empty()) {
            int y = q.front().first.first;
            int x = q.front().first.second;
            int intime = q.front().second;
 
            if (intime != time) break;
            q.pop();
 
            bool check = true;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (yy >= 0 && yy < n && xx>=0 && xx < m && arr[yy][xx] == 2) {
                    check = false;
                    break;
                }
            }
 
            if (!check) delq.push({ y,x });
            else q.push({ {y,x},time + 1 }), finish = false;
        }
 
        while (!delq.empty()) {
            int y = delq.front().first;
            int x = delq.front().second;
            delq.pop();
            arr[y][x] = 2;
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (!arr[yy][xx]) hole.push({ yy,xx });
            }
            cnt++;
        }
 
        while (!hole.empty()) {
            int y = hole.front().first;
            int x = hole.front().second;
            hole.pop();
 
            if (visit[y][x]) continue;
            visit[y][x] = true;
            arr[y][x] = 2;
 
            for (int i = 0; i < 4; i++) {
                int yy = y + my[i];
                int xx = x + mx[i];
                if (!arr[yy][xx]) hole.push({ yy,xx });
            }
        }
 
        time++;
        if (finish) {
            ans = cnt;
            break;
        }
    }
 
    cout << time << " " << ans;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 2011번 암호코드  (0) 2020.02.13
BOJ 2638번 치즈  (0) 2020.02.13
BOJ 13711번 LCS 4  (0) 2020.02.12
BOJ 1958번 LCS 3  (0) 2020.02.12
BOJ 9252번 LCS2  (0) 2020.02.12

BOJ : https://www.acmicpc.net/problem/13711

github : https://github.com/junho0956/Algorithm/blob/master/13711/13711/%EC%86%8C%EC%8A%A4.cpp

 

처음에는 LCS와 똑같이 풀면된다고 생각했는데

길이가 10만이여서 메모리초과가 발생할 것 같아 생각을 바꾸었습니다.

어차피 DP 배열은 굳이 N^2 가 아닌 2*N 만 있어도 가능하니, 2*N 으로 작성하였습니다.

그런데 10만이면 2*N 로 만들어봤자 시간초과가 발생할 것 같았습니다.

 

문제를 다시보니 1~N 의 모든수가 주어진다는 힌트가 있었습니다.

2개의 스트링을 비교할때는 스트링이 포함하는 모든 문자가 같다는 보장이 없지만

이 문제는 문자 즉, 모든 숫자를 서로 포함하고 있다는 조건이 있습니다.

 

이의 경우 임의의 배열이 가지는 숫자가 다른 배열에서는 몆번째 순서를 가지는지 알 수 있다면,

LCS가 아닌 가장 긴 증가하는 부분수열인 LIS 를 통해서 해답을 구할 수 있습니다.

 

ex)

A: 4 3 1 5 2

B: 5 2 1 4 3 일때

A 배열의 숫자가 B에서는 몆번째로 등장하는지 파악한다면

C: 4 5 3 1 2 가 되고, lis 길이는 2, lis를 구현하는 집합은 4,3 이 되는 것을 알 수 있습니다.

 

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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
vector<int> dp;
int aarr[100001];
int barr[100001];
int carr[100001];
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    int N; cin >> N;
    for (int i = 1; i <= N; i++) {
        int n; cin >> n;
        aarr[n] = i;
    }
    for (int i = 0; i < N; i++) {
        cin >> barr[i];
        carr[i] = aarr[barr[i]];
    }
 
    dp.push_back(carr[0]);
    int lis = 0;
    for (int i = 1; i < N; i++) {
        if (carr[i] > dp[lis]) {
            dp.push_back(carr[i]), lis++;
        }
        else {
            int lo = lower_bound(dp.begin(), dp.end(), carr[i]) - dp.begin();
            dp[lo] = carr[i];
        }
    }
 
    cout << lis + 1;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 2638번 치즈  (0) 2020.02.13
BOJ 2636번 치즈  (0) 2020.02.13
BOJ 1958번 LCS 3  (0) 2020.02.12
BOJ 9252번 LCS2  (0) 2020.02.12
BOJ 9251번 LCS  (0) 2020.02.12

+ Recent posts