![](https://i-blog.csdnimg.cn/blog_migrate/86d7a336315b67ab615d39812f83677a.png)
3
4
1001
5
10010
7
0111011
输出
Yes
Yes
No
题解思路
使用双指针和贪心 遍历整个数组,找到与回文字符串不同的字符 进行反码 ,并进行标记,进入反码的循环,并且直接结束外循环。
献上代码
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
string a;
bool pri(int n) {
for (int i = 0, j = n-1; i < j; i++, j--) {
if (a[i] != a[j]) return false;
}
return true;
}
int main() {
int t, n, i, j;
cin >> t;
while (t--) {
bool g = true;
cin >> n;
cin >> a;
for (i = 0, j = n - 1; i < j; i++, j--) {
if (a[i] != a[j] && g) {
g = false;
for (i, j; i < j; i++, j--) {
a[i] = '1' - a[i]+'0';
}
}
}
if (pri(n))cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}