美团20240420春招第七场笔试【硬件开发方向】

美团20240420春招笔试【硬件开发方向】

编程题:

修改字母只能每次加1,都是小写字母,输入操作k次,问能否恰好k次将字符s修改为和字符串t相同

输入:

3
2 1
aa
ba
2 3
aa
ba
3 101
bbb
aaa
3 // 总共有几次修改字母
2 1 // 字符串长度为2,操作次数为1
aa // 字符串s
ba // 字符串t
2 3 // 字符串长度为2,操作次数为3
aa // 字符串s
ba // 字符串t
3 101 // 字符串长度为3,操作次数为101
bbb // 字符串s
aaa // 字符串t

输出:

// 三次修改结果
Yes
No
Yes

示例代码(不一定全对)

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution {
public:
    char correct(char a) {
        if (a < 'z') {
            return a + 1;
        }
        else {
            return 'a';
        }
    }
    vector<string> correct_string(vector<string> s_vec, vector<string> t_vec, vector<int> n_vec, vector<int> k_vec, int q) {
        vector<string> result(q, "No");
        vector<vector<int>> cnt_vec(q);
        for (int i = 0; i < q; i++) {
            int cnt = k_vec[i];
            for (int j = 0; j < n_vec[i]; j++) {
                if (s_vec[i][j] == t_vec[i][j]) {
                    cnt_vec[i].push_back(0);
                    continue;
                }
                else {
                    char temp = s_vec[i][j];
                    int cnt_1 = 0;
                    while (temp != t_vec[i][j]) {
                        temp = correct(temp);
                        cnt_1++;
                    }
                    cnt_vec[i].push_back(cnt_1);
                }
            }
            int sum = 0;
            for (int s = 0; s < n_vec[i]; s++) {
                    sum += cnt_vec[i][s];
            }
            if ((sum - cnt) % 26 == 0) {
                result[i] = "Yes";
            }
        }
        return result;
    }
};

int main() {
    int q;
    cin >> q;
    int temp = q;
    vector<string> s_vec;
    vector<string> t_vec;
    vector<int> n_vec;
    vector<int> k_vec;
    while (temp--) {
        int n, k;
        cin >> n >> k;
        n_vec.push_back(n);
        k_vec.push_back(k);
        string s;
        cin >> s;
        s_vec.push_back(s);
        string t;
        cin >> t;
        t_vec.push_back(t);
    }
    Solution sol;
    vector<string> res = sol.correct_string(s_vec, t_vec, n_vec, k_vec, q);
    for (int i = 0; i < q; i++) cout << res[i] << endl;

}
// 64 位输出请用 printf("%lld")

相关推荐

  1. 20240420笔试硬件开发方向

    2024-04-20 14:18:06       16 阅读
  2. 牛客笔试2024第一【测试方向

    2024-04-20 14:18:06       31 阅读
  3. 笔试2024年第二笔试(技术)

    2024-04-20 14:18:06       21 阅读
  4. 笔试:4.6笔试硬件开发方向

    2024-04-20 14:18:06       14 阅读
  5. - 运维开发 - 复盘(更新中)

    2024-04-20 14:18:06       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-20 14:18:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-20 14:18:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-20 14:18:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-20 14:18:06       18 阅读

热门阅读

  1. 甲辰年半日闲有得

    2024-04-20 14:18:06       13 阅读
  2. 前端的未来已然到来

    2024-04-20 14:18:06       13 阅读
  3. Spring Boot 学习(7)——条件注解

    2024-04-20 14:18:06       16 阅读
  4. Spring Boot 实现定时任务

    2024-04-20 14:18:06       15 阅读
  5. 详细理解React的Fiber结构

    2024-04-20 14:18:06       32 阅读
  6. Spring Boot定义类处理API通用返回数据

    2024-04-20 14:18:06       18 阅读
  7. 在redhat7/8平台上部署ELK7.17.18的技术方案

    2024-04-20 14:18:06       48 阅读
  8. 【Clickhouse】如何在ClickHouse中删除集群表数据

    2024-04-20 14:18:06       42 阅读
  9. [Android]Jetpack Compose设置颜色

    2024-04-20 14:18:06       20 阅读