IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C. A-B Palindrome -> 正文阅读

[C++知识库]C. A-B Palindrome

https://codeforces.com/contest/1512/problem/Care given a string?ss?consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string?ss?by '0' or '1' so that the string becomes a palindrome and has?exactly?aa?characters '0' and?exactly?bb?characters '1'. Note that each of the characters '?' is replaced?independently?from the others.

A string?tt?of length?nn?is called a palindrome if the equality?t[i]=t[n?i+1]t[i]=t[n?i+1]?is true for all?ii?(1≤i≤n1≤i≤n).

For example, if?s=s="01?????0",?a=4a=4?and?b=4b=4, then you can replace the characters '?' in the following ways:

  • "01011010";
  • "01100110".

For the given string?ss?and the numbers?aa?and?bb, replace all the characters with '?' in the string?ss?by '0' or '1' so that the string becomes a palindrome and has?exactly?aa?characters '0' and?exactly?bb?characters '1'.

Input

The first line contains a single integer?tt?(1≤t≤1041≤t≤104). Then?tt?test cases follow.

The first line of each test case contains two integers?aa?and?bb?(0≤a,b≤2?1050≤a,b≤2?105,?a+b≥1a+b≥1).

The second line of each test case contains the string?ss?of length?a+ba+b, consisting of the characters '0', '1', and '?'.

It is guaranteed that the sum of the string lengths of?ss?over all test cases does not exceed?2?1052?105.

Output

For each test case, output:

  • "-1", if you can't replace all the characters '?' in the string?ss?by '0' or '1' so that the string becomes a palindrome and that it contains?exactly?aa?characters '0' and?exactly?bb?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

Copy

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

Copy

01011010
-1
0
-1
0110
-1
111
1001
0101010
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<string.h>
using namespace std;
// ctrl+shift+C 注释
//ctrl+shift+x 取消
#define int long long
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const ll M=1e18+10;
const int mod=1e9+7;
int aa[N],sum[N];
priority_queue<int,vector<int>,greater<int> >pq;
set<int>se;
map<char,int>mp;
queue<int>qu;
vector<int>v;
deque<int>de;
int a,b;
string s,ss;
int t=0;
void solve()
{
   cin>>a>>b;
   cin>>s;
   mp.clear();
   int len=s.size();
   for(int i=0;i<len;i++)
   {
       mp[s[i]]++;
   }
   a-=mp['0'];
   b-=mp['1'];
   if(a<0||b<0)
   {
       cout<<-1<<endl;
       return;
   }
   for(int i=0,j=len-1;i<=j;i++,j--)/*必须得先跑一遍循环,优先考虑一个问号和数值的情况*/
   {
     if((s[i]=='1'&&s[j]=='0')||(s[i]=='0'&&s[j]=='1'))//构成不了回文
      {
          cout<<-1<<endl;
          return;
      }
     else if(s[i]!=s[j])
      {
         if(s[i]=='?'&&s[j]=='1')
         s[i]=s[j],b--;
         else if(s[i]=='?'&&s[j]=='0') 
          s[i]=s[j],a--;
         if(s[i]=='1'&&s[j]=='?')
          s[j]=s[i],b--;
         else if(s[i]=='0'&&s[j]=='?') 
         s[j]=s[i],a--;
         if(b<0||a<0)//这时候缺‘0’或者‘1’,明显不能构成回文
         {
             cout<<-1<<endl;
             return;
         }
      }
   }
  for(int i=0,j=len-1;i<=j;i++,j--)//再考虑两个问号的情况
  {
      if((s[i]=='1'&&s[j]=='0')||(s[i]=='0'&&s[j]=='1'))
      {
          cout<<-1<<endl;
          return;
      }
      if(s[i]=='?'&&s[j]=='?'&&i<j)
      {
          
          if(a>=2&&a>=b)//谁多用谁是最优的
          {
              s[i]='0';
              s[j]='0';
             a-=2;
          }
          else if(b>=2&&b>=a)
          {
              s[i]='1';
              s[j]='1';
               b-=2;
          }
          else
          {
              cout<<-1<<endl;
              return;
          }
      }
       else if(s[i]=='?'&&s[j]=='?'&&i==j)
       {
            if(a>0&&a>=b)
          {
              s[i]='0';
              s[j]='0';
              a-=1;
          }
            else if(b>0&&b>=a)
          {
              s[i]='1';
              s[j]='1';
              b-=1;
          }
         else
          {
              cout<<-1<<endl;
              return;
          }
       }
  }
  cout<<s<<endl;
}
signed main()
{
   cin>>t;
  for(int i=1;i<=t;i++)
   {
       solve();
   }
}

思路不难,但是细节多

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-10-17 12:13:43  更:2022-10-17 12:15:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 6:41:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码