给定两个字符串X和Y。任务是找到字符串X的最长子序列的长度,该字符串是序列Y中的子字符串。
例子:
Input : X = "ABCD", Y = "BACDBDCD"
Output : 3
"ACD" is longest subsequence of X which
is substring of Y.
Input : X = "A", Y = "A"
Output : 1
方法1(蛮力):
使用蛮力查找X的所有子序列,并为每个子序列检查它是否为Y的子串。如果它是Y的子字符串,则保持最大长度变量并将其与长度进行比较。
方法2:(动态编程):
令n为X的长度,m为Y的长度。创建一个m + 1行和n + 1列的2D数组’dp [] []’。值dp [i] [j]是X [0….j]的子序列的最大长度,它是Y [0….i]的子串。现在,对于dp [] []的每个单元格,填充值如下:
for (i = 1 to m)
for (j = 1 to n)
if (x[i-1] == y[j - 1])
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = dp[i][j-1];
最后,作为y子串的x最长子序列的长度为max(dp [i] [n]),其中1 <= i <= m。
下面是这种方法的实现:
C/C++
// C++ program to find maximum length of
// subsequence of a string X such it is
// substring in another string Y.
#include
#define MAX 1000
using namespace std;
// Return the maximum size of substring of
// X which is substring in Y.
int maxSubsequenceSubstring(char x[], char y[],
int n, int m)
{
int dp[MAX][MAX];
// Initialize the dp[][] to 0.
for (int i = 0; i <= m; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = 0;
// Calculating value for each element.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
// If alphabet of string X and Y are
// equal make dp[i][j] = 1 + dp[i-1][j-1]
if (x[j - 1] == y[i - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
// Else copy the previous value in the
// row i.e dp[i-1][j-1]
else
dp[i][j] = dp[i][j - 1];
}
}
// Finding the maximum length.
int ans = 0;
for (int i = 1; i <= m; i++)
ans = max(ans, dp[i][n]);
return ans;
}
// Driver Program
int main()
{
char x[] = "ABCD";
char y[] = "BACDBDCD";
int n = strlen(x), m = strlen(y);
cout << maxSubsequenceSubstring(x, y, n, m);
return 0;
}
Java
// Java program to find maximum length of
// subsequence of a string X such it is
// substring in another string Y.
public class GFG
{
static final int MAX = 1000;
// Return the maximum size of substring of
// X which is substring in Y.
static int maxSubsequenceSubstring(char x[], char y[],
int n, int m)
{
int dp[][] = new int[MAX][MAX];
// Initialize the dp[][] to 0.
for (int i = 0; i <= m; i++)
for (int j = 0; j <= n; j++)
dp[i][j] = 0;
// Calculating value for each element.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
// If alphabet of string X and Y are
// equal make dp[i][j] = 1 + dp[i-1][j-1]
if (x[j - 1] == y[i - 1])
dp[i][j] = 1 + dp[i - 1][j - 1];
// Else copy the previous value in the
// row i.e dp[i-1][j-1]
else
dp[i][j] = dp[i][j - 1];
}
}
// Finding the maximum length.
int ans = 0;
for (int i = 1; i <= m; i++)
ans = Math.max(ans, dp[i][n]);
return ans;
}
// Driver Method
public static void main(String[] args)
{
char x[] = "ABCD".toCharArray();
char y[] = "BACDBDCD".toCharArray();
int n = x.length, m = y.length;
System.out.println(maxSubsequenceSubstring(x, y, n, m));
}
}
Python3
# Python3 program to find maximum
# length of subsequence of a string
# X such it is substring in another
# string Y.
MAX = 1000
# Return the maximum size of
# substring of X which is
# substring in Y.
def maxSubsequenceSubstring(x, y, n, m):
dp = [[0 for i in range(MAX)]
for i in range(MAX)]
# Initialize the dp[][] to 0.
# Calculating value for each element.
for i in range(1, m + 1):
for j in range(1, n + 1):
# If alphabet of string
# X and Y are equal make
# dp[i][j] = 1 + dp[i-1][j-1]
if(x[j - 1] == y[i - 1]):
dp[i][j] = 1 + dp[i - 1][j - 1]
# Else copy the previous value
# in the row i.e dp[i-1][j-1]
else:
dp[i][j] = dp[i][j - 1]
# Finding the maximum length
ans = 0
for i in range(1, m + 1):
ans = max(ans, dp[i][n])
return ans
# Driver Code
x = "ABCD"
y = "BACDBDCD"
n = len(x)
m = len(y)
print(maxSubsequenceSubstring(x, y, n, m))
# This code is contributed
# by sahilshelangia
C#
// C# program to find maximum length of
// subsequence of a string X such it is
// substring in another string Y.
using System;
public class GFG
{
static int MAX = 1000;
// Return the maximum size of substring of
// X which is substring in Y.
static int maxSubsequenceSubstring(string x, string y,
int n, int m)
{
int[ ,]dp = new int[MAX, MAX];
// Initialize the dp[][] to 0.
for (int i = 0; i <= m; i++)
for (int j = 0; j <= n; j++)
dp[i, j] = 0;
// Calculating value for each element.
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
// If alphabet of string X and Y are
// equal make dp[i][j] = 1 + dp[i-1][j-1]
if (x[j - 1] == y[i - 1])
dp[i, j] = 1 + dp[i - 1, j - 1];
// Else copy the previous value in the
// row i.e dp[i-1][j-1]
else
dp[i, j] = dp[i, j - 1];
}
}
// Finding the maximum length.
int ans = 0;
for (int i = 1; i <= m; i++)
ans = Math.Max(ans, dp[i,n]);
return ans;
}
// Driver Method
public static void Main()
{
string x = "ABCD";
string y = "BACDBDCD";
int n = x.Length, m = y.Length;
Console.WriteLine(maxSubsequenceSubstring(x,
y, n, m));
}
}
// This code is contributed by vt_m.
PHP
输出:
3