CF2192B.Flipping Binary String
入门
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary string s of length n. You can perform the following operation on the string:
- Choose an index i, and flip the bit present at all other indices except for the index i. In other words, choose an integer i (1≤i≤n), and for all j such that 1≤j≤n and j=i, if sj=0, then set sj:=1, otherwise set sj:=0.
You can perform the operation any number of times, but each index can be chosen by at most one operation.
Your task is to make all bits present in the string s equal 0, or report that it is impossible to do so. You don't have to minimize the number of operations.
给你一个长度为 n 的二进制字符串 s。你可以对字符串执行以下操作:
- 选择一个下标 i,并将除下标 i 外所有位置上的比特翻转。换言之,选择一个整数 i(满足 1≤i≤n),并对所有满足 1≤j≤n 且 j=i 的 j,若 sj=0,则令 sj:=1;否则令 sj:=0。
你可以执行该操作任意多次,但每个下标最多只能被选中一次。
你的任务是使字符串 s 中所有比特均变为 0,或者判断这是不可能的。你无需最小化操作次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105).
The second line of each test case contains the binary string s of length n.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)。
每个测试用例的第二行包含一个长度为 n 的二进制字符串 s。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output −1 if it is impossible to transform all bits to 0. Otherwise, output two lines in the following format:
- In the first line, print the number of operations x (0≤x≤n).
- In the second line of each test case, print x numbers – the indices you select in each operation in order. You should guarantee that each index is chosen at most once.
If there are multiple possible solutions, you may output any.
对于每个测试用例,如果无法将所有位变为 0,则输出 −1;否则,按以下格式输出两行:
- 第一行输出操作次数 x(0≤x≤n);
- 每个测试用例的第二行输出 x 个数字——即每次操作所选的下标(按操作顺序)。需保证每个下标至多被选择一次。
若存在多种可行解,输出任意一种即可。
输入输出样例
输入#1
4 3 101 3 100 4 0000 4 1010
输出#1
1 2 -1 0 2 1 3
说明/提示
In the first test case, performing the operation on index 2 means flipping bits present at index 1 and 3. So the new string formed will be 000.
In the second test case, it can be shown that we cannot make string a equal to 000 using the described operations.
在第一个测试用例中,对索引 2 执行操作意味着翻转位于索引 1 和 3 处的比特位。因此,得到的新字符串为 000。
在第二个测试用例中,可以证明:无法通过题目所述的操作将字符串 a 变为 000。
输入解题思路,AI测评打分。不知道怎么写?