문제) 백준 - 동적 계획법 - 공통 부분 문자열
https://www.acmicpc.net/problem/5582
5582번: 공통 부분 문자열
두 문자열이 주어졌을 때, 두 문자열에 모두 포함된 가장 긴 공통 부분 문자열을 찾는 프로그램을 작성하시오. 어떤 문자열 s의 부분 문자열 t란, s에 t가 연속으로 나타나는 것을 말한다. 예를 들
www.acmicpc.net
LCS랑 유사한 연속된 공통 문자열을 찾는 문제였습니다. LCS를 풀어봤다면 쉽게 풀 수 있는 문제입니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
#define ll long long | |
#define INF 987654321 | |
#define MAX 4002 | |
using namespace std; | |
typedef pair<int, double> p; | |
string s1, s2; | |
int cache[MAX][MAX] = {0,}; | |
int main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
cin >> s1 >> s2; | |
int res = 0; | |
for (int i = 1; i <= s1.size(); ++i) { | |
for (int j = 1; j <= s2.size(); ++j) { | |
if (s1[i - 1] == s2[j - 1]) { | |
cache[i][j] = cache[i - 1][j - 1] + 1; | |
res = max(cache[i][j], res); | |
} | |
} | |
} | |
cout << res; | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 9205번 - 맥주 마시면서 걸어가기 (C++) 문제 및 풀이 (0) | 2021.12.26 |
---|---|
[백준] 2636번 - 치즈 (C++) 문제 및 풀이 (0) | 2021.12.23 |
[백준] 10803번 정사각형 만들기 (C++) 문제 및 풀이 (0) | 2021.12.20 |
[백준] 1213번 - 팰린드롬 만들기 (C++) 문제 및 풀이 (0) | 2021.12.20 |
[백준] 1043번 - 거짓말 (C++) 문제 및 풀이 (0) | 2021.12.16 |
댓글