diff --git a/yuyu0830/LCS/9252.cpp b/yuyu0830/LCS/9252.cpp new file mode 100644 index 0000000..64e6aa9 --- /dev/null +++ b/yuyu0830/LCS/9252.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +int arr[1001][1001] = {0, }; +string str1, str2; + +void print(int x, int y) { + if (!arr[x][y]) return; + + if (str1[x - 1] == str2[y - 1]) { + print(x - 1, y - 1); + printf("%c", str1[x - 1]); + return; + } + + arr[x - 1][y] > arr[x][y - 1] ? print(x - 1, y) : print(x, y - 1); +} + +int main() { + cin >> str1 >> str2; + + int len1 = str1.size(), len2 = str2.size(); + + for (int i = 0; i <= len1; i++) { + for (int j = 0; j <= len2; j++) { + if (!i || !j) arr[i][j] = 0; + else if (str1[i - 1] == str2[j - 1]) arr[i][j] = arr[i - 1][j - 1] + 1; + else arr[i][j] = max(arr[i][j - 1], arr[i - 1][j]); + } + } + + printf("%d\n", arr[len1][len2]); + print(len1, len2); +} \ No newline at end of file diff --git a/yuyu0830/README.md b/yuyu0830/README.md index 96f05e2..82d80b5 100644 --- a/yuyu0830/README.md +++ b/yuyu0830/README.md @@ -23,4 +23,5 @@ | 19차시 | 2024.07.12 | 재귀 | [우수마을](https://www.acmicpc.net/problem/1949) | - | | 20차시 | 2024.07.22 | LIS | [가장 긴 증가하는 부분 수열 5](https://www.acmicpc.net/problem/14003) | - | | 21차시 | 2024.07.23 | 수학 | [1의 개수 세기](https://www.acmicpc.net/problem/9527) | - | +| 23차시 | 2024.08.05 | LCS | [LCS 2](https://www.acmicpc.net/problem/9252) | - | --- \ No newline at end of file