CF1809A.Garland
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have a garland consisting of 4 colored light bulbs, the color of the i-th light bulb is si.
Initially, all the light bulbs are turned off. Your task is to turn all the light bulbs on. You can perform the following operation any number of times: select a light bulb and switch its state (turn it on if it was off, and turn it off if it was on). The only restriction on the above operation is that you can apply the operation to a light bulb only if the previous operation was applied to a light bulb of a different color (the first operation can be applied to any light bulb).
Calculate the minimum number of operations to turn all the light bulbs on, or report that this is impossible.
你有一串由 4 个彩色灯泡组成的彩灯,第 i 个灯泡的颜色为 si。
初始时,所有灯泡均处于关闭状态。你的任务是将所有灯泡都打开。你可以执行以下操作任意多次:选择一个灯泡并切换其状态(若原为关闭则打开,若原为打开则关闭)。对上述操作的唯一限制是:你只能对与上一次操作所选灯泡颜色不同的灯泡执行该操作(第一次操作可作用于任意灯泡)。
请计算将所有灯泡打开所需的最少操作次数;若不可能实现,请报告这一点。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The single line of each test case contains s — a sequence of 4 characters, where each character is a decimal digit. The i-th character denotes the color of the i-th light bulb.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的单行包含字符串 s —— 一个由 4 个字符组成的序列,其中每个字符均为十进制数字。第 i 个字符表示第 i 个灯泡的颜色。
输出格式
For each test case, print one integer — the minimum number of operations to turn all the light bulbs on. If it is impossible to turn all the bulbs on, print -1.
对于每个测试用例,输出一个整数——将所有灯泡都打开所需的最少操作次数。如果无法将所有灯泡都打开,则输出 −1。
输入输出样例
输入#1
3 9546 0000 3313
输出#1
4 -1 6
说明/提示
In the first example, all the colors are different, so you can just turn all the bulbs on in 4 operations.
In the second example, it is impossible to turn all the bulbs on, because after you switch one light bulb, it is impossible to turn the others on.
In the third example, you can proceed as follows: turn the first light bulb on, turn the third light bulb on, turn the fourth light bulb on, turn the third light bulb off, turn the second light bulb on, turn the third light bulb on.
在第一个例子中,所有颜色均不相同,因此你只需执行 4 次操作即可打开所有灯泡。
在第二个例子中,不可能打开所有灯泡,因为一旦你切换了一个灯泡的状态,就无法再打开其余的灯泡。
在第三个例子中,你可以按如下步骤操作:打开第一个灯泡,打开第三个灯泡,打开第四个灯泡,关闭第三个灯泡,打开第二个灯泡,打开第三个灯泡。
输入解题思路,AI测评打分。不知道怎么写?