Skip to content

Commit

Permalink
LCS 2
Browse files Browse the repository at this point in the history
  • Loading branch information
0u-Y committed Jun 22, 2024
1 parent ea290a0 commit 54d757d
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions 양영우/9252.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <algorithm>

using namespace std;



int dp[1001][1001];






int main(){

string str_a, str_b;

string result = "";

cin>>str_a;
cin>>str_b;

int len_a = str_a.size();
int len_b = str_b.size();

for(int i=0; i<=len_a; i++){
for(int j=0; j<=len_b; j++){
dp[i][j] = 0;
}
}


for(int i=1; i<=len_a; i++){
for(int j=1; j<=len_b; j++){
if(str_a[i-1] == str_b[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
}
else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}

int lcs_a = len_a;
int lcs_b = len_b;



while(len_a > 0 && len_b > 0){
if(str_a[len_a - 1] == str_b[len_b-1]){
result += str_a[len_a-1];
len_a--;
len_b--;
}
else{
if(dp[len_a-1][len_b] > dp[len_a][len_b-1]) len_a--;
else len_b--;
}
}


reverse(result.begin(), result.end());



cout<<dp[lcs_a][lcs_b]<<'\n';

cout<<result;












}

0 comments on commit 54d757d

Please sign in to comment.