Vlad is planning to hold mm 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 nn problems, where the ii-th problem has a difficulty level of aiai. 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 mm rounds.
For example, if m=1m=1, n=10n=10, a=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 tt (1≤t≤10001≤t≤1000) — the number of test cases.
The first line of each test case contains two integers nn and mm (1≤n≤501≤n≤50, 1≤m≤51≤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 aa of nn 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 mm rounds.
Examples
| Inputcopy | Outputcopy |
|---|---|
3 10 1 BGECDCBDED 10 2 BGECDCBDED 9 1 BBCDEFFGG |
2 5 1 |
思路:
本题是求从A~G为一个循环,若组成m个环,最少需要补充多少字母。
想要得到一个循环,则需要A~G各一个,故只需要统计每个字母的数量就行,若一个字母的数量少于m个,则答案增加m减去该字母的数量
代码
#include <iostream>
using namespace std;
int main()
{
int t;
cin >>t;
while(t--)
{
int a[10] = {0};
int n,m;
cin >>n>>m;
for(int i=0;i<n;i++)
{
char c;
cin >>c;
if(c == 'A') a[0]++;
else if(c == 'B') a[1]++;
else if(c == 'C') a[2]++;
else if(c == 'D') a[3]++;
else if(c == 'E') a[4]++;
else if(c == 'F') a[5]++;
else if(c == 'G') a[6]++;
}
int ans = 0;
for(int i=0;i<7;i++)
{
// cout <<a[i]<<" ";
if(a[i] < m) ans += m - a[i];
}
// cout <<endl;
cout <<ans<<endl;
}
return 0;
}
本题知道如何统计数字的数量就可以解决
1095

被折叠的 条评论
为什么被折叠?



