原文:
给定一个序列,打印它最长的回文子序列。 例:
input : bbabcbcab
output : babcbab
the above output is the longest
palindromic subsequence of given
sequence. "bbbbb" and "bbcbb" are
also palindromic subsequences of
the given sequence, but not the
longest ones.
input : geeksforgeeks
output : output can be either eekee
or eesee or eegee, ..
我们在下面的帖子中讨论了一个pg电子试玩链接的解决方案,以找到最长回文子序列的长度。 本文讨论了打印最长回文子序列的pg电子试玩链接的解决方案。 这个问题接近。事实上,我们可以用 lcs 作为子程序来解决这个问题。以下是使用 lcs 的两步pg电子试玩链接的解决方案。 1)反转给定的序列,并将反转存储在另一个数组中,比如 rev[0..n-1] 2)给定序列的 lcs 和 rev[]将是最长的回文序列。 3)一旦找到 lcs,就可以 。 以下是上述方法的实施:
c
/* cpp program to print longest palindromic
subsequence */
#include
using namespace std;
/* returns lcs x and y */
string lcs(string &x, string &y)
{
int m = x.length();
int n = y.length();
int l[m 1][n 1];
/* following steps build l[m 1][n 1] in bottom
up fashion. note that l[i][j] contains
length of lcs of x[0..i-1] and y[0..j-1] */
for (int i=0; i<=m; i )
{
for (int j=0; j<=n; j )
{
if (i == 0 || j == 0)
l[i][j] = 0;
else if (x[i-1] == y[j-1])
l[i][j] = l[i-1][j-1] 1;
else
l[i][j] = max(l[i-1][j], l[i][j-1]);
}
}
// following code is used to print lcs
int index = l[m][n];
// create a string length index 1 and
// fill it with \0
string lcs(index 1, '\0');
// start from the right-most-bottom-most
// corner and one by one store characters
// in lcs[]
int i = m, j = n;
while (i > 0 && j > 0)
{
// if current character in x[] and y
// are same, then current character
// is part of lcs
if (x[i-1] == y[j-1])
{
// put current character in result
lcs[index-1] = x[i-1];
i--;
j--;
// reduce values of i, j and index
index--;
}
// if not same, then find the larger of
// two and go in the direction of larger
// value
else if (l[i-1][j] > l[i][j-1])
i--;
else
j--;
}
return lcs;
}
// returns longest palindromic subsequence
// of str
string longestpalsubseq(string &str)
{
// find reverse of str
string rev = str;
reverse(rev.begin(), rev.end());
// return lcs of str and its reverse
return lcs(str, rev);
}
/* driver program to test above function */
int main()
{
string str = "geeksforgeeks";
cout << longestpalsubseq(str);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java program to print longest palindromic
//subsequence
class gfg {
/* returns lcs x and y */
static string lcs(string a, string b) {
int m = a.length();
int n = b.length();
char x[] = a.tochararray();
char y[] = b.tochararray();
int l[][] = new int[m 1][n 1];
/* following steps build l[m 1][n 1] in bottom
up fashion. note that l[i][j] contains
length of lcs of x[0..i-1] and y[0..j-1] */
for (int i = 0; i <= m; i ) {
for (int j = 0; j <= n; j ) {
if (i == 0 || j == 0) {
l[i][j] = 0;
} else if (x[i - 1] == y[j - 1]) {
l[i][j] = l[i - 1][j - 1] 1;
} else {
l[i][j] = math.max(l[i - 1][j], l[i][j - 1]);
}
}
}
// following code is used to print lcs
int index = l[m][n];
// create a string length index 1 and
// fill it with \0
char[] lcs = new char[index 1];
// start from the right-most-bottom-most
// corner and one by one store characters
// in lcs[]
int i = m, j = n;
while (i > 0 && j > 0) {
// if current character in x[] and y
// are same, then current character
// is part of lcs
if (x[i - 1] == y[j - 1]) {
// put current character in result
lcs[index - 1] = x[i - 1];
i--;
j--;
// reduce values of i, j and index
index--;
} // if not same, then find the larger of
// two and go in the direction of larger
// value
else if (l[i - 1][j] > l[i][j - 1]) {
i--;
} else {
j--;
}
}
string ans = "";
for (int x = 0; x < lcs.length; x ) {
ans = lcs[x];
}
return ans;
}
// returns longest palindromic subsequence
// of str
static string longestpalsubseq(string str) {
// find reverse of str
string rev = str;
rev = reverse(rev);
// return lcs of str and its reverse
return lcs(str, rev);
}
static string reverse(string str) {
string ans = "";
// convert string to character array
// by using tochararray
char[] try1 = str.tochararray();
for (int i = try1.length - 1; i >= 0; i--) {
ans = try1[i];
}
return ans;
}
/* driver program to test above function */
public static void main(string[] args) {
string str = "geeksforgeeks";
system.out.println(longestpalsubseq(str));
}
}
python 3
# python3 program to print longest
# palindromic subsequence
# returns lcs x and y
def lcs_(x, y) :
m = len(x)
n = len(y)
l = [[0] * (n 1)] * (m 1)
# following steps build l[m 1][n 1]
# in bottom up fashion. note that
# l[i][j] contains length of lcs of
# x[0..i-1] and y[0..j-1]
for i in range(n 1) :
for j in range(n 1) :
if (i == 0 or j == 0) :
l[i][j] = 0;
elif (x[i - 1] == y[j - 1]) :
l[i][j] = l[i - 1][j - 1] 1;
else :
l[i][j] = max(l[i - 1][j],
l[i][j - 1]);
# following code is used to print lcs
index = l[m][n];
# create a string length index 1 and
# fill it with \0
lcs = ["\n "] * (index 1)
# start from the right-most-bottom-most
# corner and one by one store characters
# in lcs[]
i, j= m, n
while (i > 0 and j > 0) :
# if current character in x[] and y
# are same, then current character
# is part of lcs
if (x[i - 1] == y[j - 1]) :
# put current character in result
lcs[index - 1] = x[i - 1]
i -= 1
j -= 1
# reduce values of i, j and index
index -= 1
# if not same, then find the larger of
# two and go in the direction of larger
# value
elif(l[i - 1][j] > l[i][j - 1]) :
i -= 1
else :
j -= 1
ans = ""
for x in range(len(lcs)) :
ans = lcs[x]
return ans
# returns longest palindromic
# subsequence of str
def longestpalsubseq(string) :
# find reverse of str
rev = string[: : -1]
# return lcs of str and its reverse
return lcs_(string, rev)
# driver code
if __name__ == "__main__" :
string = "geeksforgeeks";
print(longestpalsubseq(string))
# this code is contributed by ryuga
c
// c# program to print longest palindromic
//subsequence
using system;
public class gfg {
/* returns lcs x and y */
static string lcs(string a, string b) {
int m = a.length;
int n = b.length;
char []x = a.tochararray();
char []y = b.tochararray();
int [,]l = new int[m 1,n 1];
int i, j;
/* following steps build l[m 1,n 1] in bottom
up fashion. note that l[i,j] contains
length of lcs of x[0..i-1] and y[0..j-1] */
for (i = 0; i <= m; i ) {
for (j = 0; j <= n; j ) {
if (i == 0 || j == 0) {
l[i,j] = 0;
} else if (x[i - 1] == y[j - 1]) {
l[i,j] = l[i - 1,j - 1] 1;
} else {
l[i,j] = math.max(l[i - 1,j], l[i,j - 1]);
}
}
}
// following code is used to print lcs
int index = l[m,n];
// create a string length index 1 and
// fill it with \0
char[] lcs = new char[index 1];
// start from the right-most-bottom-most
// corner and one by one store characters
// in lcs[]
i = m; j = n;
while (i > 0 && j > 0) {
// if current character in x[] and y
// are same, then current character
// is part of lcs
if (x[i - 1] == y[j - 1]) {
// put current character in result
lcs[index - 1] = x[i - 1];
i--;
j--;
// reduce values of i, j and index
index--;
} // if not same, then find the larger of
// two and go in the direction of larger
// value
else if (l[i - 1,j] > l[i,j - 1]) {
i--;
} else {
j--;
}
}
string ans = "";
for (int x = 0; x < lcs.length; x ) {
ans = lcs[x];
}
return ans;
}
// returns longest palindromic subsequence
// of str
static string longestpalsubseq(string str) {
// find reverse of str
string rev = str;
rev = reverse(rev);
// return lcs of str and its reverse
return lcs(str, rev);
}
static string reverse(string str) {
string ans = "";
// convert string to character array
// by using tochararray
char[] try1 = str.tochararray();
for (int i = try1.length - 1; i >= 0; i--) {
ans = try1[i];
}
return ans;
}
/* driver program to test above function */
public static void main() {
string str = "geeksforgeeks";
console.write(longestpalsubseq(str));
}
}
// this code is contributed by 29ajaykumar
java 描述语言
输出:
eegee
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处