美团校招机试 - 小美的MT(20240309-T3)

题目来源

美团校招笔试真题_小美的MT

题目描述

MT 是美团的缩写,因此小美很喜欢这两个字母。

现在小美拿到了一个仅由大写字母组成字符串,她可以最多操作 k 次,每次可以修改任意一个字符。

小美想知道,操作结束后最多共有多少个 'M' 和 'T' 字符?

输入描述

第一行输入两个正整数 n,k,代表字符串长度和操作次数。

第二行输入一个长度为 n 的、仅由大写字母组成的字符串。

  • 1 ≤ k ≤ n ≤ 10^5

输出描述

输出操作结束后最多共有多少个 'M' 和 'T' 字符。

用例

|----|--------------------------------------------|
| 输入 | 5 2 MTUAN |
| 输出 | 4 |
| 说明 | 修改第三个和第五个字符,形成的字符串为 MTTAM,这样共有 4 个'M'和'T'。 |

题目解析

本题我们只需要统计出第二行输入的s串中:

M和T字符的数量:mt_count,以及非M、T字符的数量:not_mt_count。

由于我们最多可以操作k次,即最多将k个非M、T字符修改为M或T字符:

  • 若 not_mt_count >= k,则我们最多增加 k 个 M或T 字符
  • 若 not_mt_count < k, 则我们最多增加 not_mt_count 个 M或T 字符

即最终最多有 mt_count + min(k, not_mt_count) 个 M或T 字符。

JS算法源码

javascript 复制代码
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  const [n, k] = (await readline()).split(" ").map(Number);
  const s = await readline();

  let mt_count = 0;
  let not_mt_count = 0;

  for (let c of s) {
    if (c == "M" || c == "T") {
      mt_count++;
    } else {
      not_mt_count++;
    }
  }

  const ans = Math.min(k, not_mt_count) + mt_count;

  console.log(ans);
})();

Java算法源码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();
        String s = sc.next();

        int mt_count = 0;
        int not_mt_count = 0;

        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);

            if (c == 'M' || c == 'T') {
                mt_count++;
            } else {
                not_mt_count++;
            }
        }

        int ans = Math.min(k, not_mt_count) + mt_count;

        System.out.println(ans);
    }
}

Python算法源码

python 复制代码
if __name__ == "__main__":
    n, k = map(int, input().split())
    s = input()

    mt_count = 0
    not_mt_count = 0

    for c in s:
        if c == "M" or c == "T":
            mt_count += 1
        else:
            not_mt_count += 1

    ans = min(k, not_mt_count) + mt_count

    print(ans)

C算法源码

cpp 复制代码
#include <stdio.h>
#include <math.h>

#define MAX_LEN 100001

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    char s[MAX_LEN];
    scanf("%s", s);

    int mt_count = 0;
    int not_mt_count = 0;

    int i = 0;
    while (s[i] != '\0') {
        if (s[i] == 'M' || s[i] == 'T') {
            mt_count++;
        } else {
            not_mt_count++;
        }
        i++;
    }

    int ans = (int) fmin(k, not_mt_count) + mt_count;

    printf("%d\n", ans);

    return 0;
}

C++算法源码

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    int mt_count = 0;
    int not_mt_count = 0;

    for (const auto &c: s) {
        if (c == 'M' || c == 'T') {
            mt_count++;
        } else {
            not_mt_count++;
        }
    }

    int ans = min(k, not_mt_count) + mt_count;

    cout << ans << endl;

    return 0;
}
相关推荐
钱栈up5 小时前
三星盯上散热老玩家Modine:AI算力链最后一块拼图要进自家口袋
爬虫·python·数据分析
sharemylove11 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
FYKJ_201011 小时前
springboot众创项目团队管理系统94666-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spark·课程设计
摇滚侠13 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 20
java·笔记·python
IvanCodes14 小时前
模型如何自我改进?拆解 RSI 的模型权重与 Harness 两条路线
人工智能·算法·ai·论文·agent·rsi
罗西的思考14 小时前
【轻量化复刻大模型 / KIMI K3】MXFP4 / MXFP8 量化感知训练(QAT)& 演示
人工智能·算法·机器学习
维爱智VR认知学能15 小时前
VR学能软件离线断线与乱序事件测试:确定性到达表与重放
python·单元测试·软件工程·分布式系统·离线同步
郝学胜-神的一滴15 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt
FYKJ_201015 小时前
springboot贫困生认定管理系统59009-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·后端·django·课程设计
Thanks_ks16 小时前
SQL 注入是怎么发生的:从拼接 SQL 讲到预编译语句的防御原理
python·sqlite·orm·占位符·sql 注入·预编译语句·白名单校验