【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 format

Two strings a and b with a newline separating them

Constraints

All characters are upper-cased and lie between ascii values 65-90The maximum length of the strings is 5000

Output format

Length of the string S

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 and SALLY 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 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


import java.util.Scanner;
import java.lang.String;
import java.lang.Math;

/*
class TreeNode
{
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x) { val = x; left = null; right = null;}
}
class ListNode
{
	int val;
	ListNode next;
	ListNode(int x){val = x; next = null;}
}
*/
public class Solution {

	public static void main(String[] args)
	{
		//int T;
		Scanner jin = new Scanner(System.in);
		//T = jin.nextInt();
		String str1 = jin.next();
		String str2 = jin.next();
		int len = str1.length();
		int[][] array = new int[len+1][len+1];
		for(int i = 0; i < len+1; i++)
			array[0][i] = array[i][0] = 0;
		for(int i = 0; i < len; i++)
		{
			for(int j = 0; j < len; j++)
			{
				if (str1.charAt(i) == str2.charAt(j)) {
					array[i+1][j+1] = array[i][j] + 1;
				}
				else {
					array[i+1][j+1] = Math.max(array[i][j+1], array[i+1][j]);
				}
			}
		}
		System.out.println(array[len][len]);
		array = null;
	}

}
时间: 2024-11-14 15:15:55

【HackerRank】Common Child (LCS)最长公共子序列的相关文章

LCS 最长公共子序列(DP经典问题)

最长公共子序列问题以及背包问题都是DP(动态规划)算法的经典题目,值得深度挖掘以致了解DP算法思想.问题如下: 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最

lcs(最长公共子序列),dp

lcs(最长公共子序列) 求两个序列的lcs的长度,子序列可不连续 dp[i][j]=dp[i-1][j-1]+1(a[i]==b[i]) dp[i][j]=max(dp[i-1][j],dp[i][j-1])(a[i]!=b[i]) memset(dp,0,sizeof(dp)); for(int i=1;i<=n1;i++){ for(int j=1;j<=n2;j++){ if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(

HDU 1159:Common Subsequence(最长公共子序列)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23108    Accepted Submission(s): 10149 Problem Description A subsequence of a given sequence is the given sequence with some e

算法设计 - LCS 最长公共子序列&amp;&amp;最长公共子串 &amp;&amp;LIS 最长递增子序列

出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的最长公共子串方法.最长公共子串用动态规划可实现O(n^2)的时间复杂度,O(n^2)的空间复杂度:还可以进一步优化,用后缀数组的方法优化成线性时间O(nlogn):空间也可以用其他方法优化成线性.3.LIS(最长递增序列)DP方法可实现O(n^2)的时间复杂度,进一步优化最佳可达到O(nlogn)

POJ - 1458 Common Subsequence (LCS最长公共子序列)

题意: 给出两个字符串,求出最长的公共子序列大小. 思路: 算是最经典的LCS问题吧. 设 \(X=(x_1,x_2,.....x_n) 和 Y=(y_1,y_2,.....y_m)\) 是两个序列,将 X 和 Y 的最长公共子序列记为\(lcs(X,Y)\) ,找出\(lcs(X,Y)\)就是一个最优问题 然后我们需要将其分解成子问题并求出子问题的最优解: (寻找子问题来推出当前问题,正是寻找状态转移方程最重要的一步) 1)如果 \(x_n=y_m\),即X的最后一个元素与Y的最后一个元素相同

hdu 1159 Common Subsequence(最长公共子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37551    Accepted Submission(s): 17206 Problem Description A subsequence of

lightoj 1110 - An Easy LCS 最长公共子序列+string

1110 - An Easy LCS PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB LCS means 'Longest Common Subsequence' that means two non-empty strings are given; you have to find the Longest Common Subsequence between them. Since there

POJ 2250(LCS最长公共子序列)

compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is n

LIS(最长递增子序列)和LCS(最长公共子序列)的总结

最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1, len2) s1[i - 1] == s2[j - 1], dp[i][j] = dp[i - 1][j -1] + 1; s1[i - 1] != s2[j - 1], dp[i][j] = max (dp[i - 1][j], dp[i][j - 1]); 初始化:dp[i][0] = dp[0][j] = 0; 伪代码: dp[maxn1][ma