HackerRank - Common Child

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;
}
时间: 2024-11-08 18:26:09

HackerRank - Common Child的相关文章

寒假。3.3.G - Common Child (最大公共子序)

Given two strings a and b of equal length, what's the longest string (s) that can be constructed such that it is a child of both? A string x is said to be a child of a string y if x can be formed by deleting 0 or more characters from y. For example, 

【HackerRank】Common Child (LCS)最长公共子序列

Given two strings a and b of equal length, what's the longest string (S) that can be constructed such that S is a child to both a and b. String x is said to be a child of string y if x can be formed by deleting 0 or more characters from y Input forma

Common Child

题目如下: Sample Input #0 HARRY SALLY Sample Output #0 2 The longest possible subset of characters that is possible by deleting zero or more characters from HARRY andSALLY is AY, whose length is 2. Sample Input #1 AA BB Sample Output #1 0 AA and BB has n

[HackerRank] The Longest Common Subsequence

This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here. My accepted code is as follows. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5

ThinkPHP/Common/extend.php

<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2010 http://thinkphp.cn All ri

POJ 1470 Closest Common Ancestors LCA题解

本题也是找LCA的题目,不过要求多次查询,一般的暴力查询就必然超时了,故此必须使用更高级的方法,这里使用Tarjan算法. 本题处理Tarjan算法,似乎输入处理也挺麻烦的. 注意: 因为查询的数据会极大,故此使用一个数组记录所有查询数据就会超时的.我就载在这里了.查了好久才想到这点.因为我使用了一个vector容器记录了查询数据,故此每次都循环这组这么大的数据,就超时了.----解决办法:使用一个vector<int> quest来记录查询数组,这样每次都只需要循环某节点的邻接查询点就可以了

POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Submit Status Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figu

【HackerRank】Closest Numbers

Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses also. Challenge Given a list of unsorted numbers, can you find the numbers that have the smallest absol

通过update-mapping更新child的field失败——NullPointerException

使用Elasticsearch时,需要用到parent-child API,建立parent的mapping后,动态更新child的field字段时,出现常见的NEP--NullPointerException.log日志异常如下: [2016-04-01 12:04:53,986][INFO ][rest.suppressed          ] /test/_mapping/name/ Params: {index=test, type=name} java.lang.NullPointe