C. Qingshan Loves Strings 2
Qingshan has a string s s s which only contains 0 \texttt{0} 0 and 1 \texttt{1} 1.
A string a a a of length k k k is good if and only if
- a i ≠ a k − i + 1 a_i \ne a_{k-i+1} ai=ak−i+1 for all i = 1 , 2 , … , k i=1,2,\ldots,k i=1,2,…,k.
For Div. 2 contestants, note that this condition is different from the condition in problem B.
For example, 10 \texttt{10} 10, 1010 \texttt{1010} 1010, 111000 \texttt{111000} 111000 are good, while 11 \texttt{11} 11, 101 \texttt{101} 101, 001 \texttt{001} 001, 001100 \texttt{001100} 001100 are not good.
Qingshan wants to make s s s good. To do this, she can do the following operation at most 300 300 300 times (possibly, zero):
- insert 01 \texttt{01} 01 to any position of s s s (getting a new s s s).
Please tell Qingshan if it is possible to make s s s good. If it is possible, print a sequence of operations that makes s s s good.
Input
The input consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1\le t\le 100 1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 100 1 \le n\le 100 1≤n≤100) — the length of string s s s, respectively.
The second line of each test case contains a string s s s with length n n n.
It is guaranteed that s s s only consists of 0 \texttt{0} 0 and 1 \texttt{1} 1.
Output
For each test case, if it impossible to make s s s good, output − 1 -1 −1.
Otherwise, output p p p ( 0 ≤ p ≤ 300 0 \le p \le 300 0≤p≤300) — the number of operations, in the first line.
Then, output p p p integers in the second line. The i i i-th integer should be an index x i x_i xi ( 0 ≤ x i ≤ n + 2 i − 2 0 \le x_i \le n+2i-2 0≤xi≤n+2i−2) — the position where you want to insert 01 \texttt{01} 01 in the current s s s. If x i = 0 x_i=0 xi=0, you insert 01 \texttt{01} 01 at the beginning of s s s. Otherwise, you insert 01 \texttt{01} 01 immediately after the x i x_i xi-th character of s s s.
We can show that under the constraints in this problem, if an answer exists, there is always an answer that requires at most 300 300 300 operations.
Example
input
6
2
01
3
000
4
1111
6
001110
10
0111001100
3
001
output
0
-1
-1
2
6 7
1
10
-1
Note
In the first test case, you can do zero operations and get s = 01 s=\texttt{01} s=01, which is good.
Another valid solution is to do one operation: (the inserted 01 \texttt{01} 01 is underlined)
- 0 01 ‾ 1 \texttt{0}\underline{\texttt{01}}\texttt{1} 0011
and get s = 0011 s = \texttt{0011} s=0011, which is good.
In the second and the third test case, it is impossible to make s s s good.
In the fourth test case, you can do two operations:
- 001110 01 ‾ \texttt{001110}\underline{\texttt{01}} 00111001
- 0011100 01 ‾ 1 \texttt{0011100}\underline{\texttt{01}}\texttt{1} 0011100011
and get s = 0011100011 s = \texttt{0011100011} s=0011100011, which is good.
分析
要求好题的最少操作次数,我们可以进行的操作为向任意位置插入 01 01 01
好题代表的是字符串满足 a i ! = a k − i + 1 a_i != a_{k - i + 1} ai!=ak−i+1
并且题目给出限制如果操作次数超过了300还不是好题的话,那么直接打印 − 1 -1 −1
思路
优先考虑 − 1 -1 −1的情况,我们可以从操作次数 ≥ 300 \geq 300 ≥300 入手
当操作次数等于300的时候,我们想字符串S( s i z e = 100 size = 100 size=100)中添加了600个字符
所以我们只需要判断字符串是否大于700,如果大于的话不满足
或者是
我们用一个cnt来记录操作了多少次,当操作次数 ≥ 300 \geq 300 ≥300 输出 − 1 -1 −1(推荐)
考虑正常情况
我们可以使用双指针,一个指针指头,另一个指针指尾部
判断两个位置上对应的字符是否相同,相同我们需要判断两种情况
如果 a i = a j = 1 a_i = a_j = 1 ai=aj=1,说明我们需要向 i i i前面插入使第一个元素变成0,与尾部的1配为一对
如果 a i = a j = 0 a_i = a_j = 0 ai=aj=0,说明我们需要向j后面插入
因为字符串默认插入是想下标的前面进行插入,所以向后面插入需要 j + 1 j + 1 j+1
同时也需要将 j + = i j += i j+=i,因为字符串的长度发生了变化
题目还需要插入插入位置的下标,不要忘了
#include <bits/stdc++.h>
using namespace std;
#define str string
inline void solve() {
int n; str s; cin >> n >> s;
int i = 0,j = n - 1,cnt = 0,ans;
vector<int> sp;
for(ans = 0;i <= j;i++,j--,cnt++) {
if(cnt > 300) {cout << -1 << endl; return;}
char c1 = s[i],c2 = s[j];
if(c1 == c2) {
if(c1 == '1') {s.insert(i,"01"); sp.push_back(i);}
else {s.insert(j + 1,"01"); sp.push_back(j + 1);}
j += 2;
ans++;
}
// cout << s << endl;
}
cout << ans << endl;
for(auto it : sp) cout << it << " ";
cout << endl;
}
int main(){
// freopen("input.txt","r",stdin);
int t; cin >> t;
while(t--) solve();
// solve();
return 0;
}