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

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

 

LCS 의 개념을 정리한 포스팅입니다.

LCS : https://junho0956.tistory.com/39?category=868069

 

LCS2보다 오히려 쉬운 문제입니다. LCS의 개념을 안다는 가정하에말이죠

2차원에서 3차원으로 늘어난 것 뿐이고, 3중포문이지만 100*100*100 에 if문 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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
 
int dp[101][101][101];
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
 
    string fs, ss, ts;
    cin >> fs >> ss >> ts;
 
    for (int i = 0; i < fs.length(); i++) {
        for (int j = 0; j < ss.length(); j++) {
            for (int k = 0; k < ts.length(); k++) {
                if (fs[i] == ss[j] && ss[j] == ts[k]) dp[i + 1][j + 1][k + 1= dp[i][j][k] + 1;
                else dp[i + 1][j + 1][k + 1= max(dp[i][j + 1][k + 1], max(dp[i + 1][j][k + 1], dp[i + 1][j + 1][k]));
            }
        }
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

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

BOJ 2636번 치즈  (0) 2020.02.13
BOJ 13711번 LCS 4  (0) 2020.02.12
BOJ 9252번 LCS2  (0) 2020.02.12
BOJ 9251번 LCS  (0) 2020.02.12
BOJ 5502번 팰린드롬  (0) 2020.02.12

+ Recent posts