You are given a string s consisting of the characters ‘0’, ‘1’, and ‘?’. You need to replace all the characters with ‘?’ in the string s by ‘0’ or ‘1’ so that the string becomes a palindrome and has exactly a characters ‘0’ and exactly b characters ‘1’. Note that each of the characters ‘?’ is replaced independently from the others.
A string t of length n is called a palindrome if the equality t[i]=t[n−i+1] is true for all i (1≤i≤n).
For example, if s=“01???0”, a=4 and b=4, then you can replace the characters ‘?’ in the following ways:
“01011010”;
“01100110”.
For the given string s and the numbers a and b, replace all the characters with ‘?’ in the string s by ‘0’ or ‘1’ so that the string becomes a palindrome and has exactly a characters ‘0’ and exactly b characters ‘1’.
Input
The first line contains a single integer t (1≤t≤104). Then t test cases follow.
The first line of each test case contains two integers a and b (0≤a,b≤2⋅105, a+b≥1).
The second line of each test case contains the string s of length a+b, consisting of the characters ‘0’, ‘1’, and ‘?’.
It is guaranteed that the sum of the string lengths of s over all test cases does not exceed 2⋅105.
Output
For each test case, output:
“-1”, if you can’t replace all the characters ‘?’ in the string s by ‘0’ or ‘1’ so that the string becomes a palindrome and that it contains exactly a characters ‘0’ and exactly b characters ‘1’;
the string that is obtained as a result of the replacement, otherwise.
If there are several suitable ways to replace characters, you can output any.
Example Input
9
4 4
01???0
3 3
???
1 0
?
2 2
0101
2 2
01?0
0 1
0
0 3
1?1
2 2
?00?
4 3
??010?0
Output
01011010
-1
0
-1
0110
-1
111
1001
0101010
题意:?可以变0和1,输入a个0,b个1,求能不能变成一个回文串,并且最后0的个数=a,1的个数=b。
题解:先把能确定的先确定,不能确定的再根据0和1 确定,n分两种,n为偶数,n为奇数,当n为奇数的时候,会出现i和j相等的情况,所以要考虑0的个数是加1还是加2;
就是纯纯的暴力模拟一下,代码挺长的。
AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=2*1e5+10;
char s[maxn];
int main()
{
int t;
scanf("%d",&t);
int i,j,k;
int a,b,n;
int s0,s1;
int flag;
while(t--)
{
s0=0,s1=0,flag=0;
scanf("%d%d",&a,&b);
n=a+b;
getchar();
for(i=1; i<=n; i++)
scanf("%c",&s[i]);
if(n==1)
{
if(s[1]=='?')
{
if(a==1)
{
s[1]='0';
s0++;
}
else
{
s[1]='1';
s1++;
}
}
else
{
if(s[1]=='0')
s0++;
else
s1++;
}
if(s0==a&&s1==b)
printf("%c\n",s[1]);
else
printf("-1\n");
continue;
}
if(n%2==0)//当n为偶数
{
k=n/2;
for(i=1,j=n; i<=k; i++,j--)
{
if(s[i]!='?'&&s[j]!='?')
{
if(s[i]!=s[j])
{
flag=1;
break;
}
else
{
if(s[i]=='0')
s0+=2;
else
s1+=2;
}
}
else if(s[i]=='?'&&s[j]!='?')
{
s[i]=s[j];
if(s[j]=='0')
s0+=2;
else
s1+=2;
}
else if(s[i]!='?'&&s[j]=='?')
{
s[j]=s[i];
if(s[i]=='0')
s0+=2;
else
s1+=2;
}
else
continue;
}
for(i=1,j=n; i<=k; i++,j--)
{
if(s[i]=='?'&&s[j]=='?')
{
if((s0+2)<=a)
{
s[i]='0',s[j]='0';
s0+=2;
}
else
{
s[i]='1',s[j]='1';
s1+=2;
}
}
if(s1>b)
{
flag=1;
break;
}
}
}
else
{
k=(n/2)+1;
for(i=1,j=n; i<=k; i++,j--)
{
if(s[i]!='?'&&s[j]!='?')
{
if(s[i]!=s[j])
{
flag=1;
break;
}
else
{
if(i==j)
{
if(s[i]=='0')
s0++;
else
s1++;
}
else
{
if(s[i]=='0')
s0+=2;
else
s1+=2;
}
}
}
else if(s[i]=='?'&&s[j]!='?')//i和j肯定不相等
{
s[i]=s[j];
if(s[j]=='0')
s0+=2;
else
s1+=2;
}
else if(s[i]!='?'&&s[j]=='?')//i和j肯定不相等
{
s[j]=s[i];
if(s[i]=='0')
s0+=2;
else
s1+=2;
}
else
continue;
}
for(i=1,j=n; i<=k; i++,j--)
{
if(s[i]=='?'&&s[j]=='?')
{
if(i==j)
{
if((s0+1)<=a)
{
s[i]='0';
s0++;
}
else
{
s[i]='1';
s1++;
}
if(s1>b)
{
flag=1;
break;
}
}
else
{
if((s0+2)<=a)
{
s[i]='0',s[j]='0';
s0+=2;
}
else
{
s[i]='1',s[j]='1';
s1+=2;
}
if(s1>b)
{
flag=1;
break;
}
}
}
}
}
if(flag==1)
printf("-1\n");
else if(s0==a&&s1==b)
{
for(i=1; i<=n; i++)
printf("%c",s[i]);
printf("\n");
}
else
printf("-1\n");
}
return 0;
}