LCS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main(void)
{
       int LCS_length = 0, max;
       int **table;
       string a, b;
       cin >> a >> b;
       a = "0" + a;
       b = "0" + b;
       int len1, len2;
       len1 = a.length();
       len2 = b.length();
       table = new int*[len2];
       for (int i = 0; i < len2; ++i) {
              table[i] = new int[len1];
       }
       for (int i = 0; i < len1; ++i) {
              table[0][i] = 0; //table에서 첫번째 열 초기화
       }
       for (int i = 1; i < len2; ++i) {
              max = 0;
              table[i][0] = 0;
              for (int j = 1; j < len1; ++j) {
                     if (a[j] == b[i]) {
                           max = table[i - 1][j - 1] + 1;
                           table[i][j] = max;
                     }
                     else {
                           if (table[i][j - 1] > table[i - 1][j])
                                  table[i][j] = table[i][j - 1];
                           else
                                  table[i][j] = table[i - 1][j];
                     }
              }
              if (LCS_length < max)
                     LCS_length = max;
       }
       cout << LCS_length << endl;
       return 0;
}

참고한 곳 

'알고리즘' 카테고리의 다른 글

[DP] 백준 14501 퇴사  (0) 2018.04.29
[DP] 백준 1520 내리막길  (0) 2018.04.29
[DP] 백준 10942 팰린드롬?  (0) 2018.04.29
[DP] 백준 1890 점프, 3372 보드 점프 다시풀만한 것  (0) 2018.04.29
[DP] 백준 2096 내려가기  (0) 2018.04.29

+ Recent posts