📌  相关文章
📜  找到一个回文字符串B 使得给定的字符串 A 是 B 的子序列

📅  最后修改于: 2021-10-26 05:39:31             🧑  作者: Mango

给定一个字符串A  .查找字符串B  ,其中B是回文,A 是 B 的子序列。

字符串的亚序列是可以从它通过删除一些(不一定是连续的)字符而不改变剩余字符的顺序来导出的字符串。例如,“costst”是“contest”的子序列。

回文是向前或向后读取相同内容的字符串。

例子

Input : A = "aba"
Output : B = aba
Explanation : "aba" is a subsequence of "aba" 
which is a palindrome.

Input : A = "ab"
Output : B = abba

方法:让 reverse(s) 成为字符串的反转s  .现在, s + reverse(s)将始终具有s  作为子序列(如前半部分),它是一个回文。
因此, B = A + reverse(A)

下面是上述方法的实现:

C++14
// C++ program to find a palindromic string B
// such that given String A is a subsequense of B
#include 
using namespace std;
 
// Function to check if a string is palindrome
bool checkPalindrome(string s)
{
    // Reversing a string
    string x = s;
    reverse(s.begin(), s.end());
 
    // check if reversed string is equal
    // to given string
    return s == x;
}
 
// Function to find a palindromic string B
// such that given String A is a subsequense of B
string findStringB(string A)
{
    // Reversing the string A
    string B = A;
    reverse(A.begin(), A.end());
    A = A + B;
 
    // If the string A is already a palindrome
    // return A
    if (checkPalindrome(B))
        return B;
 
    // else return B
    return A;
}
 
string reverse(string input)
{
    string temparray = input;
    int left, right = 0;
    right = temparray.length() - 1;
 
    for (left = 0; left < right; left++, right--)
 
        // Swap values of left and right
        swap(temparray[left], temparray[right]);
 
    return temparray;
}
 
// Driver Code
int main(int argc, char const *argv[])
{
    string A = "ab";
    cout << findStringB(A) << endl;
    return 0;
}
 
// This code is contributed by
// sanjeev2552


Java
// Java program to find a palindromic string B
// such that given String A is a subsequense of B
 
class GFG
{
 
    // Function to check if a string is palindrome
    static boolean checkPalindrome(String s)
    {
        // Reversing a string
        String x = reverse(s);
         
        // check if reversed string is equal
        // to given string
        return x.equals(s);
    }
     
    // Function to find a palindromic string B
    // such that given String A is a subsequense of B
    static String findStringB(String A)
    {
 
        // Reversing the string A
        String B = reverse(A);
        B = B + A;
 
        // If the string A is already a palindrome
        // return A
        if (checkPalindrome(A))
        {
            return A;
        }
 
        // else return B
        return B;
    }
 
    static String reverse(String input)
    {
        char[] temparray = input.toCharArray();
        int left, right = 0;
        right = temparray.length - 1;
 
        for (left = 0; left < right; left++, right--)
        {
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.valueOf(temparray);
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        String A = "ab";
        System.out.println(findStringB(A));
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python program to find a palindromic string B
# such that given String A is a subsequense of B
 
# Function to check if a string is palindrome
def checkPalindrome(s):
    # Reversing a string
    x = s[::-1]
    # check if reversed string is equal
    # to given string
    if(x == s):
        return True
    else:
        return False
         
# Function to find a palindromic string B
# such that given String A is a subsequense of B
def findStringB(A):
     
    # Reversing the string A
    B = A[::-1]
     
    B = B + A
     
    # If the string A is already a palindrome
    # return A
    if(checkPalindrome(A)):
        return A
       
    # else return B   
    return B
 
# Driver Code
A ="ab"
print(findStringB(A))


C#
// C# program to find a palindromic string B
// such that given String A is a subsequense of B
using System;
 
class GFG
{
  
    // Function to check if a string is palindrome
    static bool checkPalindrome(String s)
    {
        // Reversing a string
        String x = reverse(s);
          
        // check if reversed string is equal
        // to given string
        return x.Equals(s);
    }
      
    // Function to find a palindromic string B
    // such that given String A is a subsequense of B
    static String findStringB(String A)
    {
  
        // Reversing the string A
        String B = reverse(A);
        B = B + A;
  
        // If the string A is already a palindrome
        // return A
        if (checkPalindrome(A))
        {
            return A;
        }
  
        // else return B
        return B;
    }
  
    static String reverse(String input)
    {
        char[] temparray = input.ToCharArray();
        int left, right = 0;
        right = temparray.Length - 1;
  
        for (left = 0; left < right; left++, right--)
        {
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.Join("",temparray);
    }
      
    // Driver Code
    public static void Main(String[] args)
    {
        String A = "ab";
        Console.WriteLine(findStringB(A));
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:
baab

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程