A. Milica and String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Milica has a string � of length �, consisting only of characters A and B. She wants to modify � so it contains exactly � instances of B. In one operation, she can do the following:
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 � so it contains exactly � instances of B. She also wants you to find these operations (that is, integer � and character � selected in each operation).
Input
Each test contains multiple test cases. The first line contains the number of test cases � (1≤≤5001≤�≤500). The description of test cases follows.
The first line of each test case contains two integers � and � (3≤≤1003≤�≤100, 0≤≤0≤�≤�) — the length of the string � and the number of characters B Milica wants to appear in � in the end.
The second line of each test case contains the string � of length �, consisting only of characters A and B.
Output
For each test case, in the first line output a single integer � — the minimum number of operations Milica should perform.
In the �-th of the next � lines output an integer � (1≤≤1≤�≤�) and a character � (� is 'A' or 'B') — the parameters of the �-th operation as described in the statement.
If there are multiple solutions with the minimum possible number of operations, output any of them.
Example
input
Copy
5 5 2 AAABB 5 3 AABAB 5 0 BBBBB 3 0 BAA 10 3 BBBABBBBAB
output
Copy
0 1 1 B 1 5 A 1 2 A 1 6 A
Note
In the first test case, there are already 22 characters B in �, so Milica does not have to perform any operations.
In the second test case, the only way to achieve 33 characters B in � in one operation is to replace the first character of � by B on the first operation: AABAB →→ BABAB.
In the third test case, the only way to achieve 00 characters B in � in one operation is to replace the first 55 characters of � by A on the first operation: BBBBB →→ AAAAA.
In the fourth test case, one of the ways to achieve 00 characters B in � in one operation is to replace the first 22 characters of � by A on the first operation: BAA →→ AAA. Note that "1 A" and "3 A" are also correct one-operation solutions.
#include
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k;
scanf("%d%d",&n,&k);
string s;
cin>>s;
int cnt=count(s.begin(),s.end(),'B');
if(k==cnt) printf("0\n");
else
{
printf("1\n");
if(k>cnt)
{
for(int i=0;i
我们最多不会超过两次操作可以把原来的数组改变成我们需要的数组,因为可以第一次把所有的元素更改成A,然后第二次操作把前面k个元素更换成B
是:输入一个数字n,表示数组长度,输入一个数字k,表示我们需要的B的数量,然后输入一个长度为n的字符串,要求输出,经过多少次操作,把前面多少个元素更换成A/B可以使得更改之后的数组中B的数目刚好等于数字k
1.官方题解
2.题解
1.首先这是一道div 2的A题,所以难度其实还是不大的,主要考察的是对题目意思的解读,还有模拟实现题目的要求
2.stl的使用:count可以用来计数,使用这一行代码可以直接得到B元素在字符串中出现的次数
int cnt=count(s.begin(),s.end(),'B');
3.读入字符串的话最好直接使用cin读入,如果是字符数组的话,可以使用scanf读入,因为我们要使用count函数,所以直接使用的读入字符串,count函数的用法:传送门
4.首先特判一下,如果B的数目刚好和要求的数目相等,就输出0
5.然后考虑其他情况,操作次数一定是1次,极限情况就像提示里面说的,先把所有元素换成A,然后把前面k个元素换成B,这样操作次数是2次,但事实上我们不需要把所有元素换成A,也就是说我们操作次数只需要1次就可以实现要求,所以除去相等的情况,可以直接输出1,表示只需要1次操作
6.剩下两种情况,第一种情况是cnt小于k,需要把A更换成B,遍历整个字符串,只要该字符是A,就把cnt的数值增加1,模拟把A更换成B,直到cnt等于k,说明此时满足要求了,输出i+1,表示把前面多少个元素更改为B,这里使用i+1是因为字符数组是从0开始计数,自然计数是从1开始计数,所以从数组下标计数改成自然计数要加一,输出B,跳出循环,另一种情况cnt大于k同理