codeforces 1512 C. A-B Palindrome(1200,回文)
作者:互联网
链接:https://codeforces.com/problemset/problem/1512/C
题意:构造满足条件的字符串
题解:没啥解,就硬凑;具体看代码;
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while (t--)
{
int a, b, n;
cin >> a >> b;
n = a + b;
string s;
cin >> s;
for (int i = 0; i < n; i++)
{
if (s[i] == '?')s[i] = s[n - i - 1];
}
a -= count(s.begin(), s.end(), '0');//返回字符串中所有0的数量
b -= count(s.begin(), s.end(), '1');//返回字符串中所有1的数量
for (int i = 0; i <= n / 2; i++)
{
if (i != n - i - 1 && s[i] == '?')
{
if (a > 1)
{
s[i] = s[n - i - 1] = '0';
a -= 2;
}
else if (b > 1)
{
s[i] = s[n - i - 1] = '1';
b -= 2;
}
}
else if (s[i] == '?')//中间位置特判
{
if (a)
{
s[i] = '0';
a--;
}
else
{
s[i] = '1';
b--;
}
}
}
string t = s;
reverse(t.begin(), t.end());//反转字符串
if (t == s && a == 0 && b == 0)
cout << s << '\n';
else
cout << "-1\n";
}
return 0;
}
标签:begin,Palindrome,end,cin,int,1200,codeforces,--,else 来源: https://blog.csdn.net/m0_55858144/article/details/119395816