B. Reverse Sort

B. Reverse Sort

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Ashish has a binary string ss of length nn that he wants to sort in non-decreasing order.

He can perform the following operation:

  1. Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any kk such that 1≤k≤n1≤k≤n and any sequence of kk indices 1≤i1<i2<…<ik≤n1≤i1<i2<…<ik≤n such that si1≥si2≥…≥siksi1≥si2≥…≥sik.
  2. Reverse this subsequence in-place. Formally, swap si1si1 with siksik, swap si2si2 with sik−1sik−1, …… and swap si⌊k/2⌋si⌊k/2⌋ with si⌈k/2⌉+1si⌈k/2⌉+1 (Here ⌊x⌋⌊x⌋ denotes the largest integer not exceeding xx, and ⌈x⌉⌈x⌉ denotes the smallest integer not less than xx)

Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most nn operations.

Input

The first line contains a single integer tt (1≤t≤1000)(1≤t≤1000)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1000)(1≤n≤1000)  — the length of the binary string ss.

The second line of each test case contains a binary string ss of length nn containing only 00s and 11s.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000.

Output

For each test case output the following:

  • The minimum number of operations mm in the first line (0≤m≤n0≤m≤n).
  • Each of the following mm lines should be of the form: kk i1i1 i2i2 ... ikik, where kk is the length and i1<i2<...<iki1<i2<...<ik are the indices of the chosen subsequence. For them the conditions from the statement must hold.

Example

input

Copy

3
7
0011111
5
10100
6
001000

output

Copy

0
1
4 1 3 4 5 
1
3 3 5 6 

题目来自codeforces

先排序再比对,创建一个数组储存结果即可

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n,ans=0;
        cin >> n;
        string A;
        char a[1001],b[1001];
        int c[1000];
        cin >> A;
        strcpy_s(a, A.c_str());
        for (int i = 0; i < n; i++) {
            b[i] = a[i];
        }
        sort(a, a + n);
        b[n] = '\0';
        for (int i=0; i < n; i++) {
            if (a[i] != b[i]) {
                c[ans] = i+1;
                ans++;
            }
        } 
        if (ans) cout << 1<<endl;
        else {
            cout << 0 << endl;
            continue;
        }cout << ans<<" ";
        for(int i=0;i<ans;i++) cout << c[i] << " ";
        cout << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Krito.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值