codeforces思维题题解(950 div3 A)

原题:Problem - A - Codeforces

题解:遍历给定的字符串,用桶(哈希表)记录对应字母(ascii码 - ‘A’)的数量,即使1到7对应A到G,随后遍历一遍,如果计数大于等于m,说明题目够了,答案不变。如果计数小于m,说明题目不够,要加上m - cnt[i](cnt[i]表示i对应的字母的计数,1-7的一个数字)。最后输出答案即可。

AC代码:

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <bitset>
#include <ctime>
using namespace std;
using ll = long long;
ll INF = 4e18;
const int N = 3e5 + 10;
ll a[N], p[N], b[N], c0[N], c1[N], d[N], c[N];
pair<ll, ll> dp[N];
ll mod = 1e9 + 7;
bool vis[N];

map<ll, ll> cnt;
void solve()
{
    cnt.clear();
    int n, m; cin >> n >> m;
    string s; cin >> s;
    for (int i = 0; i < n; i++)
    {
        cnt[s[i] - 'A' + 1]++;
    }
    ll ans = 0;
    for (int i = 1; i <= 7; i++)
    {
        ans += max(0ll, m - cnt[i]);
    }
    cout << ans << "\n";
}

int main()
{
    ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);
    int _ = 1; cin >> _;
    while (_--)
        solve();

    return 0;
}

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值