CF2162B.Beautiful String
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary∗ string s of length n.
Your task is to find any subsequence† p of s such that:
- The subsequence p is non-decreasing. That is, each character in p is not greater than the next one.
- Let x denote the string obtained by removing all characters of p from s, while preserving the order of the remaining characters. Then x must be a palindrome‡.
You only need to output any valid subsequence p that satisfies both conditions. If no such subsequence exists, output −1.
Note that an empty string is both non-decreasing and a palindrome.
∗A binary string is a string consisting of characters '0' and '1'.
†A subsequence of a string s=s1s2…sn is a sequence p=si1si2…sik such that 1≤i1<i2<…<ik≤n. The characters are selected in order, but not necessarily contiguously. Note that an empty string is a subsequence of any string.
‡A string t=t1t2…tm is a palindrome if ti=tm−i+1 for all 1≤i≤m. In other words, the string reads the same forward and backward.
给你一个长度为 n 的二进制∗字符串 s。
你的任务是找出 s 的任意一个子序列† p,使得:
- 子序列 p 是非递减的,即 p 中每个字符都不大于其后一个字符;
- 设 x 为从 s 中删除 p 的所有字符(保持剩余字符的原有顺序)后所得的字符串,则 x 必须是一个回文串‡。
你只需输出任意一个满足上述两个条件的子序列 p。若不存在这样的子序列,则输出 −1。
注意:空字符串既是非递减的,也是回文串。
∗二进制字符串是指仅由字符 '0' 和 '1' 组成的字符串。
†字符串 s=s1s2…sn 的一个子序列是指形如 p=si1si2…sik 的序列,其中 1≤i1<i2<…<ik≤n。字符按原顺序选取,但不必连续。注意:空字符串是任意字符串的子序列。
‡字符串 t=t1t2…tm 是回文串,当且仅当对所有 1≤i≤m,均有 ti=tm−i+1。换言之,该字符串正读与反读完全相同。
输入格式
The first line contains a single integer t (1≤t≤3000) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤10) — the length of the string.
The second line contains a binary string s of length n.
第一行包含一个整数 t(1≤t≤3000)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤10)—— 字符串的长度。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 s。
输出格式
If a solution exists:
- On the first line, print a single integer k (0≤k≤n) — the length of the subsequence p.
- On the second line, print k distinct integers i1,i2,…,ik (1≤i1<i2<⋯<ik≤n) — the indices of the characters in s that form p (in order as they appear in s).
Otherwise, print a single line containing −1.
如果存在解:
- 在第一行,输出一个整数 k(0≤k≤n)——子序列 p 的长度。
- 在第二行,输出 k 个互不相同的整数 i1,i2,…,ik(1≤i1<i2<⋯<ik≤n)——构成 p 的字符串 s 中字符的下标(按其在 s 中出现的顺序)。
否则,输出一行,包含 −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=010, which is a palindrome.
In the second test case, we remove p=01 (indices 2, 3), resulting in x=0, which is a palindrome.
In the third test case, we remove p=00111 (indices 1 to 5), resulting in an empty string, which is trivially a palindrome.
In the fourth test case, we remove p=01 (indices 3, 4), resulting in x=110011, which is a palindrome.
In the fifth test case, we remove p=01 (indices 5, 6), resulting in x=1001, which is a palindrome.
在第一个测试用例中,我们移除一个空字符串,得到 x=010,它是一个回文串。
在第二个测试用例中,我们移除 p=01(下标为 2、3),得到 x=0,它是一个回文串。
在第三个测试用例中,我们移除 p=00111(下标为 1 至 5),得到一个空字符串,它显然也是一个回文串。
在第四个测试用例中,我们移除 p=01(下标为 3、4),得到 x=110011,它是一个回文串。
在第五个测试用例中,我们移除 p=01(下标为 5、6),得到 x=1001,它是一个回文串。
输入解题思路,AI测评打分。不知道怎么写?