美团校招机试 - 小美的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;
}
相关推荐
论迹5 分钟前
【JavaEE】-- 多线程(初阶)2
java·开发语言·java-ee
pk_xz1234566 分钟前
基于Python和Neo4j开发的医疗辅助诊断系统的详细实现步骤和代码示例
python·oracle·neo4j
桃子是唯一的水果14 分钟前
java 单例模式(Lazy Initialization)实现遍历文件夹下所有excel文件且返回其运行时间
java·单例模式·maven
+72015 分钟前
如何在java中用httpclient实现rpc post 请求
java·开发语言·rpc
ybq1951334543117 分钟前
javaEE-SpringBoot日志
java·spring boot·后端
火烧屁屁啦21 分钟前
【JavaEE进阶】图书管理系统 - 贰
java·spring
xzzd_jokelin21 分钟前
Spring AI 接入 DeepSeek:开启智能应用的新篇章
java·人工智能·spring·ai·大模型·rag·deepseek
web_1553427465623 分钟前
性能巅峰对决:Rust vs C++ —— 速度、安全与权衡的艺术
c++·算法·rust
9毫米的幻想25 分钟前
【Linux系统】—— 冯诺依曼体系结构与操作系统初理解
linux·运维·服务器·c语言·c++
Dongwoo Jeong29 分钟前
缓存基础解释与缓存友好型编程基础
后端·c·cache·cache friendly