1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

最長共通部分列問題

Last updated at Posted at 2022-03-15

問題

// 入力
string S1, T1;

// DP テーブル
int dp[1010][1010]; //[S : i番目][T : j番目]

int main() {
    cin >> S1 >> T1; //文字列

    memset(dp, 0, sizeof(dp)); // 初期化
    for (int i = 0; i < S1.size(); ++i) {
        for (int j = 0; j < T1.size(); ++j) {
            if (S1[i] == T1[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); //一致すれば
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]); //一致しなければ
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]); //一致しなければ
        }
    }

    cout << dp[S1.size()][T1.size()] << endl; //0が初期値なので
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?