原题链接:洛谷
题目描述
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]t[i]=t[n−i+1] is true for all i (1≤i≤n ).For example, if s=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’.
输入格式
The first line contains a single integer t ( 1≤t≤10^4 ). Then t test
cases follow.The first line of each test case contains two integers a and b (
0≤a,b≤2⋅10 ^5 , 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⋅10^5
输出格式
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.
题意翻译
一句话题意,给出 T 个由 0、1、? 组成的字符串,以及这个字符串由 a 个 0 和 b 个 1 组成,要求将字符串中的 ? 替换成 0
或 1 之后是一个回文串并且恰好有 a 个 0 和 b 个 1。
输入输出样例
输入 #1复制 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
输出 #1复制
01011010
-1
0
-1
0110
-1
111
1001
0101010
题解
暴力模拟
题解:先把能确定的先确定,不能确定的再根据0和1 确定,n分两种,n为偶数,n为奇数,当n为奇数的时候,会出现i和j相等的情况,所以要考虑0的个数是加1还是加2
代码样例:
#include<bits/stdc++.h>
using namespace std;
const int maxn=2*1e5+10;//数组范围
char s[maxn];//数组范围开的较大时
int main()
{
int t;
scanf("%d",&t);//输入多组数据的组数
int a,b,n;
int s0,s1;//记录‘0’‘1’出现的次数
int flag;//
while(t--)
{
s0=0,s1=0;
scanf("%d%d",&a,&b);//a的个数 b的个数
n=a+b;//提供的字符的总个数
getchar();//吸收换行
for(int i=1; i<=n; i++)
scanf("%c",&s[i]);//输入
if(n==1)//总共一个字符
{
if(s[1]=='?')//第一个字符为‘?’
{
if(a==1)//一个字符为‘0’
{
s[1]='0';
s0++;//记录‘0’出现的次数
}
else//一个字符为‘1’
{
s[1]='1';
s1++;//记录‘1’出现的次数
}
}
else//第一个字符不为‘?’
{
if(s[1]=='0')//第一个字符为‘0’
s0++;//记录‘0’出现的次数
else
s1++;//记录‘1’出现的次数
}
if(s0==a&&s1==b)//'0'的总数为a,'1'的总数为b
printf("%c\n",s[1]);
else
printf("-1\n");
continue;
}
int flag=0;//标记初始状态
if(n%2==0)//当n为偶数
{
int k=n/2;
for(int 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')//都为‘0’
s0+=2;
else//都为‘1’
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')//首位是‘0’
s0+=2;
else//首位不是‘0’
s1+=2;
}
else
continue;
}
for(int i=1,j=n; i<=k; i++,j--)
{
if(s[i]=='?'&&s[j]=='?')//首尾都为‘?’
{
if((s0+2)<=a)//*‘0’的数量加2小于等于给定的‘0’的个数
{
s[i]='0',s[j]='0';//首尾都是‘0’
s0+=2;
}
else
{
s[i]='1',s[j]='1';//首尾都是‘1’
s1+=2;
}
}
if(s1>b)//‘1’的个数大于给定的‘1’出现的次数
{
flag=1;//改变状态
break;
}
}
}
else//当n为奇数
{
int k=(n/2)+1;//中间值
for(int 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')//首尾都为‘0’
s0++;
else//首尾都为‘1’
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')//末位为‘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(int 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(int i=1; i<=n; i++)
printf("%c",s[i]);
printf("\n");
}
else
printf("-1\n");
}
return 0;
}