백준 9251 LCS

치즈·2022년 12월 9일

BOJ

목록 보기
26/45
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

string A, B;
int dp[1001][1001];

void input(){
  cin >> A;
  cin >> B;
}

void solve() {
  for (int i = 1; i <= B.length(); i++) {
    for (int j = 1; j <= A.length(); j++) {
      if (A[j - 1] == B[i - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1; // 같으면 대각선 dp값 + 1
      } 
      else{
        dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      }
    }
  }
  cout << dp[B.length()][A.length()];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  input();
  solve();
  return 0;
}
profile
차근차근 배워나가요

0개의 댓글