CF1898A.Milica and String
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Milica has a string s of length n, consisting only of characters A and B. She wants to modify s so it contains exactly k instances of B. In one operation, she can do the following:
- Select an integer i (1≤i≤n) and a character c (c is equal to either A or B).
- Then, replace each of the first i characters of string s (that is, characters s1,s2,…,si) with c.
Milica does not want to perform too many operations in order not to waste too much time on them.
She asks you to find the minimum number of operations required to modify s so it contains exactly k instances of B. She also wants you to find these operations (that is, integer i and character c selected in each operation).
米莉察有一个长度为 n 的字符串 s,其中仅包含字符 A 和 B。她希望修改 s,使其恰好包含 k 个字符 B。在一次操作中,她可以执行以下步骤:
- 选择一个整数 i(满足 1≤i≤n)和一个字符 c(c 为 A 或 B);
- 然后,将字符串 s 的前 i 个字符(即 s1,s2,…,si)全部替换为 c。
为避免花费过多时间,米莉察不希望执行过多的操作。
她请你找出使 s 恰好包含 k 个 B 所需的最少操作次数,并给出这些操作的具体方案(即每次操作所选的整数 i 和字符 c)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500). The description of test cases follows.
The first line of each test case contains two integers n and k (3≤n≤100, 0≤k≤n) — the length of the string s and the number of characters B Milica wants to appear in s in the end.
The second line of each test case contains the string s of length n, consisting only of characters A and B.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤500)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 k(3≤n≤100,0≤k≤n)——分别表示字符串 s 的长度,以及 Milica 希望最终在 s 中出现的字符 B 的个数。
每个测试用例的第二行包含一个长度为 n 的字符串 s,该字符串仅由字符 A 和 B 组成。
输出格式
For each test case, in the first line output a single integer m — the minimum number of operations Milica should perform.
In the j-th of the next m lines output an integer i (1≤i≤n) and a character c (c is 'A' or 'B') — the parameters of the j-th operation as described in the statement.
If there are multiple solutions with the minimum possible number of operations, output any of them.
对于每个测试用例,在第一行输出一个整数 m — Milica 应执行的最少操作次数。
在接下来的 m 行中,第 j 行输出一个整数 i(1≤i≤n)和一个字符 c(c 为 'A' 或 'B')—— 表示第 j 次操作的参数,其含义如题面所述。
若存在多个具有最少操作次数的解,输出其中任意一个即可。
输入输出样例
输入#1
5 5 2 AAABB 5 3 AABAB 5 0 BBBBB 3 0 BAA 10 3 BBBABBBBAB
输出#1
0 1 1 B 1 5 A 1 2 A 1 6 A
说明/提示
In the first test case, there are already 2 characters B in s, so Milica does not have to perform any operations.
In the second test case, the only way to achieve 3 characters B in s in one operation is to replace the first character of s by B on the first operation: AABAB → BABAB.
In the third test case, the only way to achieve 0 characters B in s in one operation is to replace the first 5 characters of s by A on the first operation: BBBBB → AAAAA.
In the fourth test case, one of the ways to achieve 0 characters B in s in one operation is to replace the first 2 characters of s by A on the first operation: BAA → AAA. Note that "1 A" and "3 A" are also correct one-operation solutions.
在第一个测试用例中,字符串 s 中已存在 2 个字符 B,因此 Milica 无需执行任何操作。
在第二个测试用例中,仅有一种方式能在一次操作内使 s 中恰好包含 3 个字符 B:即在第一次操作中将 s 的第一个字符替换为 B:AABAB → BABAB。
在第三个测试用例中,仅有一种方式能在一次操作内使 s 中恰好包含 0 个字符 B:即在第一次操作中将 s 的前 5 个字符全部替换为 A:BBBBB → AAAAA。
在第四个测试用例中,存在多种方式能在一次操作内使 s 中恰好包含 0 个字符 B;其中一种方式是:在第一次操作中将 s 的前 2 个字符替换为 A:BAA → AAA。注意,“替换前 1 个字符为 A”和“替换前 3 个字符为 A”同样是正确的一次操作解法。
输入解题思路,AI测评打分。不知道怎么写?