题目大意
给出一个字符串 s s s,随意排列,使其恰好有 k k k 对 s i = s n − i + 1 s_i = s_{n - i + 1} si=sn−i+1。
解析
我们已知要满足 n / 2 − k n / 2 - k n/2−k 对字符不等,则再不等的范围内需要满足 0 0 0 和 1 1 1 的个数相等,相等范围内,需要满足 0 0 0 和 1 1 1 的个数都能被 2 2 2 整除。
Code
#include <bits/stdc++.h>
using namespace std;
int t;
void solve()
{
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
int cnt0 = 0, cnt1 = 0;
string s;
cin >> s;
for (int i = 0; i < n; i++)
{
if (s[i] == '0') cnt0++;
else cnt1++;
}
int num = n / 2 - k;
cnt0 -= num;
cnt1 -= num;
if (cnt0 < 0 || cnt1 < 0)
{
cout << "NO" << endl;
continue;
}
if (cnt0 % 2 == 1 || cnt1 % 2 == 1)
{
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}


3023

被折叠的 条评论
为什么被折叠?



