📌  相关文章
📜  给定两个字符串,检查哪个字符串首先生成回文

📅  最后修改于: 2022-05-13 01:57:08.588000             🧑  作者: Mango

给定两个字符串,检查哪个字符串首先生成回文

给定两个长度相等的字符串“A”和“B”。两个玩家玩一个游戏,他们都从各自的字符串中选择一个字符(第一个从 A 中选择,第二个从 B 中选择)并放入第三个字符串(最初是空的)。能做出第三字符串回文的玩家为赢家。如果第一个玩家先回文,则打印'A',否则打印'B'。如果字符串变空并且没有人能够制作回文,则打印'B'。
例子:

Input : A = ab
        B = ab
Output : B
First player puts 'a' (from string A)
Second player puts 'a' (from string B) 
which make palindrome. 
The result would be same even if A picks
'b' as first character.

Input : A = aba
        B = cde
Output : A

Input : A = ab
        B = cd
Output : B
None of the string will be able to
make a palindrome (of length > 1)
in any situation. So B will win.

在举几个例子之后,我们可以观察到,“A”(或第一个玩家)只有在其字符出现多次且未出现在“B”中时才能获胜。

C++
// Given two strings, check which string
// makes palindrome first.
#include
using namespace std;
 
const int MAX_CHAR = 26;
 
// returns winner of two strings
char stringPalindrome(string A, string B)
{
    // Count frequencies of characters in
    // both given strings
    int countA[MAX_CHAR] = {0};
    int countB[MAX_CHAR] = {0};
    int l1 = A.length(), l2 = B.length();
    for(int i=0; i1 && countB[i] == 0))
           return 'A';
 
    return 'B';
}
 
// Driver Code
int main()
{
    string a = "abcdea";
    string b = "bcdesg";
    cout << stringPalindrome(a,b);
    return 0;
}


Java
// Java program to check which string
// makes palindrome first.
public class First_Palin {
 
    static final int MAX_CHAR = 26;
 
    // returns winner of two strings
    static char stringPalindrome(String A, String B)
    {
        // Count frequencies of characters in
        // both given strings
        int[] countA = new int[MAX_CHAR];
        int[] countB = new int[MAX_CHAR];
 
        int l1 = A.length();
        int l2 = B.length();
         
        for (int i = 0; i < l1; i++)
            countA[A.charAt(i) - 'a']++;
         
        for (int i = 0; i < l2; i++)
            countB[B.charAt(i) - 'a']++;
 
        // Check if there is a character that
        // appears more than once in A and does
        // not appear in B
        for (int i = 0; i < 26; i++)
            if ((countA[i] > 1 && countB[i] == 0))
                return 'A';
 
        return 'B';
    }
 
    // Driver Code
public static void main(String args[])
    {
        String a = "abcdea";
        String b = "bcdesg";
        System.out.println(stringPalindrome(a, b));
    }
}
// This code is contributed by Sumit Ghosh


Python3
# Given two strings, check which string
# makes palindrome first.
 
MAX_CHAR = 26
 
# returns winner of two strings
def stringPalindrome(A, B):
     
    # Count frequencies of characters
    # in both given strings
    countA = [0] * MAX_CHAR
    countB = [0] * MAX_CHAR
    l1 = len(A)
    l2 = len(B)
    for i in range(l1):
        countA[ord(A[i]) - ord('a')] += 1
    for i in range(l2):
        countB[ord(B[i]) - ord('a')] += 1
 
    # Check if there is a character that
    # appears more than once in A and
    # does not appear in B
    for i in range(26):
        if ((countA[i] > 1 and countB[i] == 0)):
            return 'A'
    return 'B'
 
# Driver Code
if __name__ == '__main__':
    a = "abcdea"
    b = "bcdesg"
    print(stringPalindrome(a, b))
 
# This code is contributed by Rajput-Ji


C#
// C# program to check which string
// makes palindrome first.
using System;
 
class First_Palin {
 
    static int MAX_CHAR = 26;
 
    // returns winner of two strings
    static char stringPalindrome(string A, string B)
    {
        // Count frequencies of characters in
        // both given strings
        int[] countA = new int[MAX_CHAR];
        int[] countB = new int[MAX_CHAR];
 
        int l1 = A.Length;
        int l2 = B.Length;
         
        for (int i = 0; i < l1; i++)
            countA[A[i] - 'a']++;
         
        for (int i = 0; i < l2; i++)
            countB[B[i] - 'a']++;
 
        // Check if there is a character that
        // appears more than once in A and does
        // not appear in B
        for (int i = 0; i < 26; i++)
            if ((countA[i] > 1 && countB[i] == 0))
                return 'A';
 
        return 'B';
    }
 
    // Driver Code
    public static void Main()
    {
        string a = "abcdea";
        string b = "bcdesg";
    Console.WriteLine(stringPalindrome(a, b));
    }
}
 
// This code is contributed by vt_m.


PHP
 1 && $countB[$i] == 0))
        return 'A';
 
    return 'B';
}
 
    // Driver Code
    $a = "abcdea";
    $b = "bcdesg";
    echo stringPalindrome($a,$b);
     
// This code is contributed by mits
?>


Javascript


输出:

A