给定N个二进制字符串b1,b2,b3…。 bn。任务是查找可以通过交换任意数量的字符对多次来使回文形成的最大二进制字符串数。字符可以来自相同的字符串,也可以来自不同的字符串
例子:
Input: N=3
1110
100110
010101
Output: 2
Explanation:
b1 = 1110
b2 = 100110 - > 110010
b3 = 010101 -> 110010
Now swap last 0 in s2 and s3
with 1's in s1
Final string become
b1 = 1000
b2 = 110011
b3 = 110011
where b1 and b2 are a palindrome
Input: N=3
1
1000
111110
Output: 3
方法:
字符串的长度不变。同样重要的是要观察到,如果给我们一个奇数长度的二进制字符串,那么我们总是可以以将字符串转换为回文的方式交换字符。这是因为,如果长度为奇数,则我们将拥有(零个偶数和一个奇数个)或(一个无偶数和零个奇数)。因此,可以始终以使该字符串回文的方式放置该字符串。
现在我们的ans可以是N或N-1。我们必须考虑ans为N的情况。因此,我们得到N个二进制字符串。如果至少有1个奇数长度的字符串,那么我们的ans肯定为N。
- 可以解释如下:
- 抓住所有0和1,并将它们从它们的斑点中移出。然后,我们将至少有一对0或1,然后将它们对称地放入其自由点(跳过奇数长度的中间)。因此,到现在为止,所有偶数长度的字符串都已填充,奇数长度的字符串在中间有一个自由点,可以很容易地用其余字符填充。因此,在这种情况下,我们的ans将为N。
- 现在,另一种情况。如果我们分别拥有所有N个偶数长度的字符串,且总数不为1和0为偶数(即1的总数为偶数,而0的总数为偶数),则在这种情况下,我们的ans也将为N。这是因为1和0可以对称地放置在所有N个字符串,以使它们回文。否则,我们的ans将为N-1。
下面是上述方法的实现:
C++
// C++ program for the
// above approach
#include
using namespace std;
int max_palindrome(string s[], int n)
{
int flag = 0;
for (int i = 0; i < n; i++) {
// To check if there is
// any string of odd length
if (s[i].size() % 2 != 0) {
flag = 1;
}
}
// If there is at least
// 1 string of odd
// length.
if (flag == 1) {
return n;
}
int z = 0, o = 0;
// If all the strings are
// of even length.
for (int i = 0; i < n; i++) {
for (int j = 0; j < s[i].size(); j++) {
// Count of 0's in all
// the strings
if (s[i][j] == '0')
z++;
// Count of 1's in
// all the strings
else
o++;
}
}
// If z is even
// and o is even
// then ans will be N.
if (o % 2 == 0 && z % 2 == 0) {
return n;
}
// Otherwise ans will be N-1.
else {
return n - 1;
}
}
// Driver code
int main()
{
int n = 3;
string s[n] = { "1110", "100110", "010101" };
cout << max_palindrome(s, n);
return 0;
}
Java
// Java program for the above approach
class GFG
{
static int max_palindrome(String []s, int n)
{
int flag = 0;
for (int i = 0; i < n; i++)
{
// To check if there is
// any string of odd length
if (s[i].length() % 2 != 0)
{
flag = 1;
}
}
// If there is at least
// 1 string of odd
// length.
if (flag == 1)
{
return n;
}
int z = 0;
int o = 0;
// If all the strings are
// of even length.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < s[i].length(); j++)
{
// Count of 0's in all
// the strings
if (s[i].charAt(j) == '0')
z += 1;
// Count of 1's in
// all the strings
else
o += 1;
}
}
// If z is even
// and o is even
// then ans will be N.
if (o % 2 == 0 && z % 2 == 0)
{
return n;
}
// Otherwise ans will be N-1.
else
{
return n - 1;
}
}
// Driver code
public static void main (String[] args)
{
int n = 3;
String s[] = {"1110", "100110", "010101" };
System.out.println(max_palindrome(s, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program for the above approach
def max_palindrome(s, n) :
flag = 0;
for i in range(n) :
# To check if there is
# any string of odd length
if (len(s[i]) % 2 != 0) :
flag = 1;
# If there is at least
# 1 string of odd
# length.
if (flag == 1) :
return n;
z = 0; o = 0;
# If all the strings are
# of even length.
for i in range(n) :
for j in range(len(s[i])) :
# Count of 0's in all
# the strings
if (s[i][j] == '0') :
z += 1;
# Count of 1's in
# all the strings
else :
o += 1;
# If z is even
# and o is even
# then ans will be N.
if (o % 2 == 0 and z % 2 == 0) :
return n;
# Otherwise ans will be N-1.
else :
return n - 1;
# Driver code
if __name__ == "__main__" :
n = 3;
s = [ "1110", "100110", "010101" ];
print(max_palindrome(s, n));
# This code is contributed by AnkitRai01
C#
// C# program for the above approach
using System;
class GFG
{
static int max_palindrome(string []s, int n)
{
int flag = 0;
for (int i = 0; i < n; i++)
{
// To check if there is
// any string of odd length
if (s[i].Length % 2 != 0)
{
flag = 1;
}
}
// If there is at least
// 1 string of odd
// length.
if (flag == 1)
{
return n;
}
int z = 0;
int o = 0;
// If all the strings are
// of even length.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < s[i].Length; j++)
{
// Count of 0's in all
// the strings
if (s[i][j] == '0')
z += 1;
// Count of 1's in
// all the strings
else
o += 1;
}
}
// If z is even
// and o is even
// then ans will be N.
if (o % 2 == 0 && z % 2 == 0)
{
return n;
}
// Otherwise ans will be N-1.
else
{
return n - 1;
}
}
// Driver code
public static void Main ()
{
int n = 3;
string []s = {"1110", "100110", "010101" };
Console.WriteLine(max_palindrome(s, n));
}
}
// This code is contributed by AnkitRai01
输出:
2