티스토리 뷰

백준

백준 소스코드 [C++] 9251 LCS

Hani_Levenshtein 2020. 12. 16. 19:06

www.acmicpc.net/problem/9251

 

9251번: LCS

LCS(Longest Common Subsequence, 최장 공통 부분 수열)문제는 두 수열이 주어졌을 때, 모두의 부분 수열이 되는 수열 중 가장 긴 것을 찾는 문제이다. 예를 들어, ACAYKP와 CAPCAK의 LCS는 ACAK가 된다.

www.acmicpc.net

백준 소스코드 [C++] 9251 LCS

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
typedef long long ll;
using namespace std;
int arr[1001][1001];
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	string s, f;
	cin >> f;
	cin >> s;
	int a = s.size(), b = f.size();
	memset(arr, 0, sizeof(arr));
	for(int i=1;i<=a;i++)
		for (int j = 1;j <= b;j++) 
			if (s[i - 1] == f[j - 1]) arr[i][j] = arr[i - 1][j - 1] + 1;
			else arr[i][j] = max(arr[i-1][j],arr[i][j-1]);
	cout << arr[a][b];
	return 0;
}
댓글