问题:
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;
}