[BOJ] 9251 LCS

Time Lapse :4min 59sec

9251.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1001][1001];
int lcs(string &a, string &b) {
for(int i = 1; i <= a.length(); i++)
for(int j = 1; j <=b.length(); j++)
dp[i][j] = a[i-1] == b[j-1] ? dp[i-1][j-1] + 1 : max(dp[i-1][j], dp[i][j-1]);
return dp[a.length()][b.length()];
}
int main() {
string a, b;
cin>>a>>b;
cout<<lcs(a,b);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/01/PS/BOJ/9251/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.