A. Problem Generator

问题:

A. Problem Generator

Vlad is planning to hold m rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.

Vlad already has a bank of n problems, where the i-th problem has a difficulty level of ai. There may not be enough of these problems, so he may have to come up with a few more problems.

Vlad wants to come up with as few problems as possible, so he asks you to find the minimum number of problems he needs to come up with in order to hold m rounds.For example, if m=1, n=10, a='BGECDCBDED', then he needs to come up with two problems: one of difficulty level 'A' and one of difficulty level 'F'.

Input

The first line contains a single integer t(1≤t≤1000) — the number of test cases.

The first line of each test case contains two integers n and m (1≤n≤50, 1≤m≤5) — the number of problems in the bank and the number of upcoming rounds, respectively.The second line of each test case contains a string a of n characters from 'A' to 'G' — the difficulties of the problems in the bank.

Output

For each test case, output a single integer — the minimum number of problems that need to come up with to hold m rounds.

问题逻辑:

输入两个数字n和m,n表示字符串的长度,m表示可以包含A~G的次数。

输出在原有字符串基础上需要添加多少个字符能够满足m次A~G。

解题思路:

1.定义一个数组b,用b[1]~b[7]分别表示A~G在字符串中的数量,要满足m次对应的次数,需要满足每一个A~G的字符数量等于m。

2.要想知道缺少的数量只需要遍历整个字符串,定义一个整型need,来存储缺少的数量。

代码源码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
    	int n,m;
    	cin>>n>>m;
		int b[8]={0};
		string s;
		cin>>s;
    	for(int i=0;i<n;i++)
			{
			if(s[i]=='A') b[1]++;
			if(s[i]=='B') b[2]++;
			if(s[i]=='C') b[3]++;
			if(s[i]=='D') b[4]++;
			if(s[i]=='E') b[5]++;
			if(s[i]=='F') b[6]++;
			if(s[i]=='G') b[7]++;
			}

		int need=0;
		for(int i=1;i<8;i++)
		{
			if(b[i]<m) need=need+(m-b[i]);
		}
		cout<<need<<'\n';
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值