BOJ ๋ฌธ์ œ ๋ณด๊ธฐ




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
#include <iostream>
#include <string>
using namespace std;

int dp[1001];

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);

	// input & init
    string s1, s2;
    cin >> s1 >> s2;

	// dynamic programming
    for (int i = 1; i <= s1.length(); i++)
    {
        int prev = 0;
        for (int j = 1; j <= s2.length(); j++)
        {
            int temp = dp[j];

            if (s1[i - 1] == s2[j - 1]) 
            {
                dp[j] = prev + 1;
            }
            else 
            {
                dp[j] = max(dp[j], dp[j - 1]); 
            }

            prev = temp;
        }
    }

	// output
    cout << dp[s2.length()];
    return 0;
}


image

Tags: , ,

Categories:

Updated:

Leave a comment