码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - Common Child

时间:2015-03-16 16:05:11      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

Longest Common Subsequence in disguise. Python impl. has TLE in one case. And C++ is fine.

#include <cmath>
#include <cstdio>
#include <vector>
#include <unordered_set>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    string a, b;
    cin >> a;
    cin >> b;
    size_t len = a.length();
    vector<vector<int>> dp(len + 1, vector<int>(len + 1, 0));

    for (int i = 0; i <= len; i++)
    for (int j = 0; j <= len; j++)
    {
        if (i * j == 0)    dp[i][j] = 0;
        else if (a[i - 1] == 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]);
    }

    cout << dp[len][len];
    return 0;
}

 

HackerRank - Common Child

标签:

原文地址:http://www.cnblogs.com/tonix/p/4341724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!