美团校招机试 - 小美的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;
}
相关推荐
曹牧7 小时前
Java:serialVersionUID
java·开发语言
老马啸西风7 小时前
成熟企业级技术平台 MVE-010-app 管理平台
人工智能·深度学习·算法·职场和发展
ekprada8 小时前
DAY36 复习日
开发语言·python·机器学习
Nan_Shu_6148 小时前
学习:Vue (2)
javascript·vue.js·学习
lzh_200110128 小时前
树状数组理解
算法
毕设源码-钟学长8 小时前
【开题答辩全过程】以 公寓出租系统为例,包含答辩的问题和答案
java·eclipse·echarts
Hello World呀8 小时前
Minio的替代品RustFS
java
爱笑的眼睛118 小时前
强化学习组件:超越Hello World的架构级思考与实践
java·人工智能·python·ai
历程里程碑8 小时前
C++ 6 :string类:高效处理字符串的秘密
c语言·开发语言·数据结构·c++·笔记·算法·排序算法
Boxsc_midnight8 小时前
【规范驱动的开发方式】之【spec-kit】 的安装入门指南
人工智能·python·深度学习·软件工程·设计规范