CF2162B.Beautiful String

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a binary∗^{\text{∗}} string ss of length nn.

Your task is to find any subsequence†^{\text{†}} pp of ss such that:

  • The subsequence pp is non-decreasing. That is, each character in pp is not greater than the next one.
  • Let xx denote the string obtained by removing all characters of pp from ss, while preserving the order of the remaining characters. Then xx must be a palindrome‡^{\text{‡}}.

You only need to output any valid subsequence pp that satisfies both conditions. If no such subsequence exists, output −1-1.

Note that an empty string is both non-decreasing and a palindrome.

∗^{\text{∗}}A binary string is a string consisting of characters '0' and '1'.

†^{\text{†}}A subsequence of a string s=s1s2…sns = s_1s_2\ldots s_n is a sequence p=si1si2…sikp = s_{i_1}s_{i_2}\ldots s_{i_k} such that 1≤i1<i2<…<ik≤n1 \leq i_1 \lt i_2 \lt \ldots \lt i_k \leq n. The characters are selected in order, but not necessarily contiguously. Note that an empty string is a subsequence of any string.

‡^{\text{‡}}A string t=t1t2…tmt = t_1t_2\ldots t_m is a palindrome if ti=tm−i+1t_i = t_{m - i + 1} for all 1≤i≤m1 \leq i \leq m. In other words, the string reads the same forward and backward.

给你一个长度为 nn 的二进制∗^{\text{∗}}字符串 ss。

你的任务是找出 ss 的任意一个子序列†^{\text{†}} pp,使得:

  • 子序列 pp 是非递减的,即 pp 中每个字符都不大于其后一个字符;
  • 设 xx 为从 ss 中删除 pp 的所有字符(保持剩余字符的原有顺序)后所得的字符串,则 xx 必须是一个回文串‡^{\text{‡}}。

你只需输出任意一个满足上述两个条件的子序列 pp。若不存在这样的子序列,则输出 −1-1。

注意:空字符串既是非递减的,也是回文串。

∗^{\text{∗}}二进制字符串是指仅由字符 '0' 和 '1' 组成的字符串。

†^{\text{†}}字符串 s=s1s2…sns = s_1s_2\ldots s_n 的一个子序列是指形如 p=si1si2…sikp = s_{i_1}s_{i_2}\ldots s_{i_k} 的序列,其中 1≤i1<i2<…<ik≤n1 \leq i_1 \lt i_2 \lt \ldots \lt i_k \leq n。字符按原顺序选取,但不必连续。注意:空字符串是任意字符串的子序列。

‡^{\text{‡}}字符串 t=t1t2…tmt = t_1t_2\ldots t_m 是回文串,当且仅当对所有 1≤i≤m1 \leq i \leq m,均有 ti=tm−i+1t_i = t_{m - i + 1}。换言之,该字符串正读与反读完全相同。

输入格式

The first line contains a single integer tt (1≤t≤30001 \le t \le 3000) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤101 \le n \le 10) — the length of the string.

The second line contains a binary string ss of length nn.

第一行包含一个整数 tt(1≤t≤30001 \le t \le 3000)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤101 \le n \le 10)—— 字符串的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。

输出格式

If a solution exists:

  • On the first line, print a single integer kk (0≤k≤n0 \le k \le n) — the length of the subsequence pp.
  • On the second line, print kk distinct integers i1,i2,…,iki_1, i_2, \dots, i_k (1≤i1<i2<⋯<ik≤n1 \le i_1 \lt i_2 \lt \dots \lt i_k \le n) — the indices of the characters in ss that form pp (in order as they appear in ss).

Otherwise, print a single line containing −1-1.

如果存在解:

  • 在第一行,输出一个整数 kk(0≤k≤n0 \le k \le n)——子序列 pp 的长度。
  • 在第二行,输出 kk 个互不相同的整数 i1,i2,…,iki_1, i_2, \dots, i_k(1≤i1<i2<⋯<ik≤n1 \le i_1 \lt i_2 \lt \dots \lt i_k \le n)——构成 pp 的字符串 ss 中字符的下标(按其在 ss 中出现的顺序)。

否则,输出一行,包含 −1-1。

输入输出样例

  • 输入#1

    5
    3
    010
    3
    001
    5
    00111
    8
    11010011
    6
    100101

    输出#1

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

说明/提示

In the first test case, we remove an empty string, resulting in x=010x = \texttt{010}, which is a palindrome.

In the second test case, we remove p=01p = \texttt{01} (indices 22, 33), resulting in x=0x = \texttt{0}, which is a palindrome.

In the third test case, we remove p=00111p = \texttt{00111} (indices 11 to 55), resulting in an empty string, which is trivially a palindrome.

In the fourth test case, we remove p=01p = \texttt{01} (indices 33, 44), resulting in x=110011x = \texttt{110011}, which is a palindrome.

In the fifth test case, we remove p=01p = \texttt{01} (indices 55, 66), resulting in x=1001x = \texttt{1001}, which is a palindrome.

在第一个测试用例中,我们移除一个空字符串,得到 x=010x = \texttt{010},它是一个回文串。

在第二个测试用例中,我们移除 p=01p = \texttt{01}(下标为 22、33),得到 x=0x = \texttt{0},它是一个回文串。

在第三个测试用例中,我们移除 p=00111p = \texttt{00111}(下标为 11 至 55),得到一个空字符串,它显然也是一个回文串。

在第四个测试用例中,我们移除 p=01p = \texttt{01}(下标为 33、44),得到 x=110011x = \texttt{110011},它是一个回文串。

在第五个测试用例中,我们移除 p=01p = \texttt{01}(下标为 55、66),得到 x=1001x = \texttt{1001},它是一个回文串。

输入解题思路,AI测评打分。不知道怎么写?

首页