LCS

size_t LCS(const std::string& x, const std::string& y)
{
    if (x.empty () || y.empty ()){
        return 0;
    }
    const size_t width  = x.length () + 1;
    const size_t height = y.length () + 1;
    std::vector<size_t> results(width);
    for (size_t i = 1; i != height; ++i){
        const auto key = y[i - 1];
        for (size_t j = 1; j != width; ++j){
            if (results[j] == results[j - 1]){
                if (x[j - 1] == key){
                    ++results[j];
                }
            }else{
                results[j] = std::max(results[j], results[j - 1]);
            }
        }
    }
    return results.back ();
}

 

posted @ 2016-01-16 18:12  wu_overflow  阅读(136)  评论(0编辑  收藏  举报