原文:
你有两条绳子。现在你必须按照字典顺序打印所有最长的公共子序列? 示例:
input : str1 = "abcabcaa", str2 = "acbacba"
output: ababa
abaca
abcba
acaba
acaca
acbaa
acbca
这个问题是的扩展。我们首先找到 lcs 的长度,并使用内存化(或动态编程)将所有 lcs 存储在 2d 表中。然后,我们在两个字符串中搜索从“a”到“z”(以输出排序顺序)的所有字符。如果在字符串中发现一个字符,并且该字符的当前位置指向 lcs,我们递归搜索当前 lcs 长度加 1 的所有出现。 下面是算法的实现。
c
// c program to find all lcs of two strings in
// sorted order.
#include
#define max 100
using namespace std;
// length of lcs
int lcslen = 0;
// dp matrix to store result of sub calls for lcs
int dp[max][max];
// a memoization based function that returns lcs of
// str1[i..len1-1] and str2[j..len2-1]
int lcs(string str1, string str2, int len1, int len2,
int i, int j)
{
int &ret = dp[i][j];
// base condition
if (i==len1 || j==len2)
return ret = 0;
// if lcs has been computed
if (ret != -1)
return ret;
ret = 0;
// if characters are same return previous 1 else
// max of two sequences after removing i'th and j'th
// char one by one
if (str1[i] == str2[j])
ret = 1 lcs(str1, str2, len1, len2, i 1, j 1);
else
ret = max(lcs(str1, str2, len1, len2, i 1, j),
lcs(str1, str2, len1, len2, i, j 1));
return ret;
}
// function to print all routes common sub-sequences of
// length lcslen
void printall(string str1, string str2, int len1, int len2,
char data[], int indx1, int indx2, int currlcs)
{
// if currlcs is equal to lcslen then print it
if (currlcs == lcslen)
{
data[currlcs] = '\0';
puts(data);
return;
}
// if we are done with all the characters of both string
if (indx1==len1 || indx2==len2)
return;
// here we have to print all sub-sequences lexicographically,
// that's why we start from 'a'to'z' if this character is
// present in both of them then append it in data[] and same
// remaining part
for (char ch='a'; ch<='z'; ch )
{
// done is a flag to tell that we have printed all the
// subsequences corresponding to current character
bool done = false;
for (int i=indx1; i
java 语言(一种计算机语言,尤用于创建网站)
// java program to find all lcs of two strings in
// sorted order.
class gfg
{
static int max = 100;
// length of lcs
static int lcslen = 0;
// dp matrix to store result of sub calls for lcs
static int[][] dp = new int[max][max];
// a memoization based function that returns lcs of
// str1[i..len1-1] and str2[j..len2-1]
static int lcs(string str1, string str2,
int len1, int len2, int i, int j)
{
int ret = dp[i][j];
// base condition
if (i == len1 || j == len2)
return ret = 0;
// if lcs has been computed
if (ret != -1)
return ret;
ret = 0;
// if characters are same return previous 1 else
// max of two sequences after removing i'th and j'th
// char one by one
if (str1.charat(i) == str2.charat(j))
ret = 1 lcs(str1, str2, len1, len2, i 1, j 1);
else
ret = math.max(lcs(str1, str2, len1, len2, i 1, j),
lcs(str1, str2, len1, len2, i, j 1));
return ret;
}
// function to print all routes common sub-sequences of
// length lcslen
static void printall(string str1, string str2, int len1, int len2,
char[] data, int indx1, int indx2, int currlcs)
{
// if currlcs is equal to lcslen then print it
if (currlcs == lcslen)
{
data[currlcs] = '\0';
system.out.println(new string(data));
return;
}
// if we are done with all the characters of both string
if (indx1 == len1 || indx2 == len2)
return;
// here we have to print all sub-sequences lexicographically,
// that's why we start from 'a'to'z' if this character is
// present in both of them then append it in data[] and same
// remaining part
for (char ch ='a'; ch <='z'; ch )
{
// done is a flag to tell that we have printed all the
// subsequences corresponding to current character
boolean done = false;
for (int i = indx1; i < len1; i )
{
// if character ch is present in str1 then check if
// it is present in str2
if (ch == str1.charat(i))
{
for (int j = indx2; j < len2; j )
{
// if ch is present in both of them and
// remaining length is equal to remaining
// lcs length then add ch in sub-sequence
if (ch == str2.charat(j) &&
dp[i][j] == lcslen - currlcs)
{
data[currlcs] = ch;
printall(str1, str2, len1, len2,
data, i 1, j 1, currlcs 1);
done = true;
break;
}
}
}
// if we found lcs beginning with current character.
if (done)
break;
}
}
}
// this function prints all lcs of str1 and str2
// in lexicographic order.
static void prinlalllcssorted(string str1, string str2)
{
// find lengths of both strings
int len1 = str1.length(), len2 = str2.length();
// find length of lcs
for(int i = 0; i < max; i )
{
for(int j = 0; j < max; j )
{
dp[i][j] = -1;
}
}
lcslen = lcs(str1, str2, len1, len2, 0, 0);
// print all lcs using recursive backtracking
// data[] is used to store individual lcs.
char[] data = new char[max];
printall(str1, str2, len1, len2, data, 0, 0, 0);
}
// driver code
public static void main(string[] args)
{
string str1 = "abcabcaa", str2 = "acbacba";
prinlalllcssorted(str1, str2);
}
}
// this code is contributed by divyesh072019
python 3
# python3 program to find all lcs of two strings in
# sorted order.
max=100
lcslen = 0
# dp matrix to store result of sub calls for lcs
dp=[[-1 for i in range(max)] for i in range(max)]
# a memoization based function that returns lcs of
# str1[i..len1-1] and str2[j..len2-1]
def lcs(str1, str2, len1, len2, i, j):
# base condition
if (i == len1 or j == len2):
dp[i][j] = 0
return dp[i][j]
# if lcs has been computed
if (dp[i][j] != -1):
return dp[i][j]
ret = 0
# if characters are same return previous 1 else
# max of two sequences after removing i'th and j'th
# char one by one
if (str1[i] == str2[j]):
ret = 1 lcs(str1, str2, len1, len2, i 1, j 1)
else:
ret = max(lcs(str1, str2, len1, len2, i 1, j),
lcs(str1, str2, len1, len2, i, j 1))
dp[i][j] = ret
return ret
# function to prall routes common sub-sequences of
# length lcslen
def printall(str1, str2, len1, len2,data, indx1, indx2, currlcs):
# if currlcs is equal to lcslen then print
if (currlcs == lcslen):
print("".join(data[:currlcs]))
return
# if we are done with all the characters of both string
if (indx1 == len1 or indx2 == len2):
return
# here we have to prall sub-sequences lexicographically,
# that's why we start from 'a'to'z' if this character is
# present in both of them then append it in data[] and same
# remaining part
for ch in range(ord('a'),ord('z') 1):
# done is a flag to tell that we have printed all the
# subsequences corresponding to current character
done = false
for i in range(indx1,len1):
# if character ch is present in str1 then check if
# it is present in str2
if (chr(ch)==str1[i]):
for j in range(indx2, len2):
# if ch is present in both of them and
# remaining length is equal to remaining
# lcs length then add ch in sub-sequence
if (chr(ch) == str2[j] and dp[i][j] == lcslen-currlcs):
data[currlcs] = chr(ch)
printall(str1, str2, len1, len2, data, i 1, j 1, currlcs 1)
done = true
break
# if we found lcs beginning with current character.
if (done):
break
# this function prints all lcs of str1 and str2
# in lexicographic order.
def prinlalllcssorted(str1, str2):
global lcslen
# find lengths of both strings
len1,len2 = len(str1),len(str2)
lcslen = lcs(str1, str2, len1, len2, 0, 0)
# prall lcs using recursive backtracking
# data[] is used to store individual lcs.
data = ['a' for i in range(max)]
printall(str1, str2, len1, len2, data, 0, 0, 0)
# driver program to run the case
if __name__ == '__main__':
str1 = "abcabcaa"
str2 = "acbacba"
prinlalllcssorted(str1, str2)
# this code is contributed by mohit kumar 29
c
// c# program to find all lcs of two strings in
// sorted order.
using system;
class gfg
{
static int max = 100;
// length of lcs
static int lcslen = 0;
// dp matrix to store result of sub calls for lcs
static int[,] dp = new int[max,max];
// a memoization based function that returns lcs of
// str1[i..len1-1] and str2[j..len2-1]
static int lcs(string str1, string str2,
int len1, int len2, int i, int j)
{
int ret = dp[i, j];
// base condition
if (i == len1 || j == len2)
return ret = 0;
// if lcs has been computed
if (ret != -1)
return ret;
ret = 0;
// if characters are same return previous 1 else
// max of two sequences after removing i'th and j'th
// char one by one
if (str1[i] == str2[j])
ret = 1 lcs(str1, str2, len1, len2, i 1, j 1);
else
ret = math.max(lcs(str1, str2, len1, len2, i 1, j),
lcs(str1, str2, len1, len2, i, j 1));
return ret;
}
// function to print all routes common sub-sequences of
// length lcslen
static void printall(string str1, string str2, int len1, int len2,
char[] data, int indx1, int indx2, int currlcs)
{
// if currlcs is equal to lcslen then print it
if (currlcs == lcslen)
{
data[currlcs] = '\0';
console.writeline(new string(data));
return;
}
// if we are done with all the characters of both string
if (indx1 == len1 || indx2 == len2)
return;
// here we have to print all sub-sequences lexicographically,
// that's why we start from 'a'to'z' if this character is
// present in both of them then append it in data[] and same
// remaining part
for (char ch='a'; ch<='z'; ch )
{
// done is a flag to tell that we have printed all the
// subsequences corresponding to current character
bool done = false;
for (int i = indx1; i < len1; i )
{
// if character ch is present in str1 then check if
// it is present in str2
if (ch == str1[i])
{
for (int j = indx2; j < len2; j )
{
// if ch is present in both of them and
// remaining length is equal to remaining
// lcs length then add ch in sub-sequence
if (ch == str2[j] &&
lcs(str1, str2, len1, len2, i, j) == lcslen-currlcs)
{
data[currlcs] = ch;
printall(str1, str2, len1, len2, data, i 1, j 1, currlcs 1);
done = true;
break;
}
}
}
// if we found lcs beginning with current character.
if (done)
break;
}
}
}
// this function prints all lcs of str1 and str2
// in lexicographic order.
static void prinlalllcssorted(string str1, string str2)
{
// find lengths of both strings
int len1 = str1.length, len2 = str2.length;
// find length of lcs
for(int i = 0; i < max; i )
{
for(int j = 0; j < max; j )
{
dp[i, j] = -1;
}
}
lcslen = lcs(str1, str2, len1, len2, 0, 0);
// print all lcs using recursive backtracking
// data[] is used to store individual lcs.
char[] data = new char[max];
printall(str1, str2, len1, len2, data, 0, 0, 0);
}
// driver code
static void main()
{
string str1 = "abcabcaa", str2 = "acbacba";
prinlalllcssorted(str1, str2);
}
}
// this code is contributed by divyeshrabadiya07
java 描述语言
output
ababa
abaca
abcba
acaba
acaca
acbaa
acbca
本文由 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。
如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处