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 no characters in common and hence the output is 0.

Sample Input #2

SHINCHAN
NOHARAAA

Sample Output #2

3

The largest set of characters, in order, between SHINCHAN and NOHARAAA is NHA.

Sample Input #3

ABCDEF
FBDAMN

Sample Output #3

2

BD is the longest child of these strings.

另外一些测试用例:

  • WEWOUCUIDGCGTRMEZEPXZFEJWISRSBBSYXAYDFEJJDLEBVHHKS FDAGCXGKCTKWNECHMRXZWMLRYUCOCZHJRRJBOAJOQJZZVUYXIC
  • 结果:15
  • ELGGYJWKTDHLXJRBJLRYEJWVSUFZKYHOIKBGTVUTTOCGMLEXWDSXEBKRZTQUVCJNGKKRMUUBACVOEQKBFFYBUQEMYNENKYYGUZSP
    FRVIFOVJYQLVZMFBNRUTIYFBMFFFRZVBYINXLDDSVMPWSQGJZYTKMZIPEGMVOUQBKYEWEYVOLSHCMHPAZYTENRNONTJWDANAMFRX
  • 结果:27
  • APMCTKBUKYRGZPAUVZEBVUXRGDVITOYXWQWRVCSXESMEHQLHPDJQWETAWQVSBRRNRRFDLFTRXOTKQHFTYAZSGBORDNAMUAJTPVOKERLVOLEALDQQLUDCU
IRXJHQEZBRWYPFJXNTPELEZHNJILIZVZLYQJDFYSYQNRFFAOYXHQBQVRLFDIIOGWKQIZGVELYOUKZBKMHVYGIKIPSEMWSCWYOJTHOQKMLBAIZYNAKYNCXKDTTESODDAEAHKCDHCJYAHERACMLYQHXIRDFUSRTZDNVHSYFKCSPPYSLHOGIBTNUJTZQWVTHKUNDNWZADMATSUXEISCACQNQXIHNTXGCZUGIGBDONYTUXAXFINAYGZJVDCTZCWPGFNQDPERUCNJUXIFDSQHULYPZRNUOKMLMMQAJMLKCHJMEFJVRYZIPFQOBSDPAITHGMNKROCWJEGESCGOIUOQHOYUEQNPJPBMCNRZUHOSQNSUNCSTVQVWFGMUFJZGMEUVUPH
  • JUVSDRRSHFGSSLLLZEPJDVAWDPKQBKUHHOZFFXKQMGAACZUYOMNPHWGTYZWQGSMNYXWNFYNOIVVMPZXUNKJQYBYJINBOHXUWIVRTVLEKCOPDMTKTGDBWE
CDAVPMLHQLERZHDVZJZODPSAPGSRWJXNGFEBQBLTLNDIEGFHEGHJWFOIYXRUJMODSNXUFWBIJJMXTFMUKQEYPNBTZFEJNLDNWCGQLVUQUKGZHJOKZNPMUYEQLEYNNORKJQAMSTHTBCCPQTTCPRZATWNJQJXPODRXKIWDOFUBZVSDTAPFRMXJBJMUGVRZOCDUIPXVEGMRQNKXDKNWXMTNDJSETAKVSYMJISAREEJPLRABMXJSRQNASOJNEEVAMWCFJBCIOCKMHCMYCRCGYFNZKNALDUNPUSTSWGOYHOSWRHWSMFGZDWSBXWXGVKQPHGINRKMDXEVTNNZTBJPXYNAXLWZSBUMVMJXDIKORHBIBECJNKWJJJSRLYQIKKPXSNUT

结果:155


在hackerrank刷到的题,好难~。

而且关键在于运行效率,我用的方法是递归,题目里给的几个例子还勉强能运行出结果,后面测试的时候完全运行不完。

要好好提高算法!

贴出自己的代码,有一个长一点的串,但是没有运行对。所以也不知道自己的算法的问题是只是效率不行还是逻辑本身就有问题。。

 1 int searchSubStr(string aStr, string bStr,int comLen){
 2
 3     int count = 0;
 4     int newcount = comLen;
 5     int aLen = aStr.length();
 6     int bLen = bStr.length();
 7     for (int i = 0; i < aLen; i++){
 8
 9         for (int j = 0; j < bLen; j++){
10
11             if (aStr.at(i) == bStr.at(j)){
12
13                 count =  searchSubStr(aStr.substr(i+1,aLen-i-1),bStr.substr(j+1,bLen-j-1),comLen+1);
14
15             }
16         }
17
18         if (count>newcount){
19             newcount = count;
20         }
21     }
22
23     return newcount;
24 }
时间: 2024-10-24 07:11:32

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

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 <

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

通过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

Eclipse Maven项目报错2之A child container failed during start

问题:在同事那里拿了一个Eclipse的maven项目,导入报错,主要显示的是A child container failed during start 具体错误如下 六月 02, 2018 12:08:50 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source'

Springboot启动报错:child container failed during start

Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;引入了一个hsf pom文件后后,翻到异常堆栈最下面发现是:ServletContext没有getVirtualServerName方法,原因是pom冲突,解决方案:在hsf里面先排除就得servlet-api,在引入最新的:A child container failed dur