最新华为OD机试真题-火星字符串(100分)

🍭 大家好这里是清隆学长 ,一枚热爱算法的程序员

✨ 本系列打算持续跟新华为OD-D卷的三语言AC题解

👏 感谢大家的订阅➕ 和 喜欢💗

📎在线评测链接

[ => 火星字符串(100分) <= ] (****************************************************************)

华为OD

🌍 评测功能需要 =>订阅专栏<= 后联系清隆解锁~

🍓OJ题目截图

alt

🍡 火星字符串

问题描述

K小姐最近在研究火星文明,她发现火星人使用的运算符号与地球人不同。火星人使用的运算符号有 #$,其中 # 号的优先级高于 $ 号,相同的运算符按从左往右的顺序计算。同时,火星人的运算符号与地球人的对应关系如下:

# = * + * + $ = * + +

其中 是无符号整数,地球人的公式按照 语言的规则进行计算。

现在 K小姐得到了一段火星人的字符串报文,她希望你能帮助翻译并计算结果。

输入格式

输入一行字符串,表示火星人的字符串表达式。

  • 输入的字符串由无符号整数和操作符(#$)组成,操作数与操作符之间没有任何分隔符。
  • 操作数的取值范围为 位无符号整数。
  • 保证输入以及计算结果不会出现整数溢出。
  • 保证输入的字符串为合法的求值报文。

输出格式

输出一个整数,表示火星人字符串表达式的计算结果。

样例输入

7#6$5#12

样例输出

157

数据范围

  • 字符串长度不超过
  • 操作数的取值范围为 位无符号整数。

题解

本题可以分为两个步骤来解决:

  1. 将火星人的 # 运算符替换为地球人的运算表达式。
  2. 计算替换后的地球人表达式的值。

对于步骤 1,我们可以使用正则表达式匹配 # 运算符及其左右两侧的操作数,然后根据公式 # = * + * + 进行替换。重复这个过程直到字符串中不再包含 # 运算符。

对于步骤 2,由于替换后的字符串只包含 $ 运算符,我们可以将字符串按照 $ 号分割成多个整数,然后根据公式 y2xy3$ 依次进行计算即可。

时间复杂度:,其中 为字符串的长度。每个字符最多被替换一次,因此总时间复杂度为

参考代码

  • Python
import re
import functools
def mars_calc(expr):
    while True:
        match = re.search(r'(\d+)#(\d+)', expr)
        if not match:
            break
        x, y = map(int, match.groups())
        expr = expr.replace(match.group(), str(4 * x + 3 * y + 2), 1)
    
    return functools.reduce(lambda x, y: 2 * x + y + 3, map(int, expr.split('$')))

expr = input().strip()
print(mars_calc(expr))
  • Java
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String expr = sc.next();
        System.out.println(marsCalc(expr));
        sc.close();
    }
    
    public static long marsCalc(String expr) {
        Pattern pattern = Pattern.compile("(\\d+)#(\\d+)");
        
        while (true) {
            Matcher matcher = pattern.matcher(expr);
            if (!matcher.find()) {
                break;
            }
            
            long x = Long.parseLong(matcher.group(1));
            long y = Long.parseLong(matcher.group(2));
            expr = expr.replaceFirst(matcher.group(), String.valueOf(4 * x + 3 * y + 2));
        }
        
        return Arrays.stream(expr.split("\\$"))
                     .mapToLong(Long::parseLong)
                     .reduce((x, y) -> 2 * x + y + 3)
                     .orElse(0);
    }
}
  • Cpp
#include <iostream>
#include <string>
#include <regex>
#include <sstream>
#include <vector>

// Function to perform the Mars calculation described in Python
int mars_calc(std::string expr) {
    std::regex pattern("(\\d+)#(\\d+)");
    std::smatch match;

    // First operation: repeatedly replace (\d+)#(\d+) with 4*x + 3*y + 2
    while (std::regex_search(expr, match, pattern)) {
        int x = std::stoi(match[1].str());
        int y = std::stoi(match[2].str());
        std::string replacement = std::to_string(4 * x + 3 * y + 2);
        expr.replace(match.position(), match.length(), replacement);
    }

    // Second operation: split by '$' and calculate 2*x + y + 3, accumulate the results
    std::stringstream ss(expr);
    std::string item;
    std::vector<int> numbers;
    while (std::getline(ss, item, '$')) {
        numbers.push_back(std::stoi(item));
    }

    int result = 0;
    if (!numbers.empty()) {
        result = numbers[0];
        for (size_t i = 1; i < numbers.size(); ++i) {
            result = 2 * result + numbers[i] + 3;
        }
    }

    return result;
}

int main() {
    std::string expr;
    std::getline(std::cin, expr);
    std::cout << mars_calc(expr) << std::endl;
    return 0;
}
#华为##华为OD##华为OD机试算法题库##华为od##华为od题库#
最新华为OD机试-D卷 文章被收录于专栏

本专栏给大家提供了华为2024最新华为OD-C/D卷的题目汇总和(Java/Cpp/Python)三语言解析 + 提供OJ在线评测

全部评论
🌍 评测功能需要 订阅专栏 后联系清隆解锁~ #华为od#
点赞
送花
回复 分享
发布于 07-02 19:08 浙江

相关推荐

1 1 评论
分享
牛客网
牛客企业服务