当前位置:网站首页>华为机试:报文解压缩
华为机试:报文解压缩
2022-07-17 12:35:00 【小朱小朱绝不服输】
【编程题目 | 200分】报文解压缩 [ 200 / 中等 ]
题目描述
- 为了提升数据传输的效率,会对传输的报文进行压缩处理。
- 输入一个压缩后的报文,请返回它解压后的原始报文。
- 压缩规则:n[str],表示方括号内部的 str 正好重复 n 次。
- 注意 n 为正整数(0 < n <= 100),str只包含小写英文字母,不考虑异常情况。
输入描述
输入压缩后的报文:
1)不考虑无效的输入,报文没有额外的空格,方括号总是符合格式要求的;
2)原始报文不包含数字,所有的数字只表示重复的次数 n ,例如不会出现像 5b 或 3[8] 的输入;
输出描述
解压后的原始报文
注:
1)原始报文长度不会超过1000,不考虑异常的情况
示例
输入
3[m2[c]]
输出
mccmccmcc
说明
m2[c] 解压缩后为 mcc,重复三次为 mccmccmcc
输入
10[k]2[mn3[j2[op]]]
输出
kkkkkkkkkkmnjopopjopopjopopmnjopopjopopjopop
思路分析
这道题是字符串处理的问题,同时字符串中嵌套括号,根据嵌套的括号进行报文解压缩,所以我们很容易想到用栈去解决问题。
- 首先把右括号之前的字符入栈。
- 遇到右括号时,开始进行解压缩,当栈不空的情况下开始出栈
- 出栈字符为字母时,暂存
- 出栈字符为数字时,判断如果前一个字符存在,是否仍为数字(处理两位数字,这里没有对数字100进行判断,如果需要再加一个判断即可)
- 循环num-1次累加暂存的字符串,因为本身有一次
注意:题目要求的压缩规则:n[str],str只包含小写英文字母。所以左括号左边一定是数字。
3[[m2[c]]2[a]]
则不符合压缩规则。
参考代码
import java.util.Scanner;
import java.util.Stack;
public class baoWenJieYaSuo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
Stack<Character> stack = new Stack<>();
String res = "";
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] == ']') {
// 解压缩
String tmpStr = "";
while(!stack.isEmpty()) {
char poll = stack.pop();
if (poll >= 'a' && poll <= 'z') {
// 如果出栈的为字母
tmpStr = String.valueOf(poll) + tmpStr;
} else if (Character.isDigit(poll)) {
// 如果出栈的为数字
int num = 0;
if (!stack.isEmpty() && Character.isDigit(stack.peek())) {
num = (stack.pop() - '0') * 10 + (poll - '0');
} else {
num = poll - '0';
}
String waitStr = tmpStr; // 需要将tempStr暂存起来
for (int j = 0; j < num - 1; j++) {
// 这里做 num -1 次的字符串相
tmpStr += waitStr;
}
}
}
res += tmpStr;
}
stack.push(ch[i]);
}
System.out.println(res);
}
}
欢迎在评论区指正以及留下自己的更简洁的方法。
边栏推荐
- 圆桌实录:炉边对话——如何在 Web3 实现创新
- Complete knapsack problem code template
- C语言自定义类型详解
- 什么是pytest,自动化测试必学
- R语言ggplot2可视化:使用ggpubr包的gghistogram函数可视化分组直方图、使用palette参数自定义分组直方图的条形边框颜色
- 旋转矩阵(Rotate Matrix)的性质分析(转发)
- The new energy track has high risks, so please pay attention to safety
- AutoJs学习-动态解密
- Analysis and solution of application jar package conflict in yarn environment
- 破案了卧槽---从MQ消费的逻辑怎么改代码都不生效
猜你喜欢
爱可可AI前沿推介(7.17)
Lvi-sam: laser IMU camera tight coupling mapping
Figure an introduction to the interpretable method of neural network and a code example of gnnexplainer interpreting prediction
图神经网络的可解释性方法介绍和GNNExplainer解释预测的代码示例
Blender digital twin production tutorial
【牛客刷题】/*C语言实现字符串左旋*/
中科磐云—D模块web远程代码执行漏洞解析
机械臂速成小指南(零点五):机械臂相关资源
华为无线设备配置智能漫游
What is pytest? Automated testing is a must
随机推荐
How to realize the association between interfaces in JMeter?
Huawei wireless device configuration intelligent roaming
作业:输入1-100的奇数
Microsoft OneNote tutorial, how to insert mathematical formulas in OneNote?
Convert excel table to word table, and keep the formula in Excel table unchanged
圆桌实录:炉边对话——如何在 Web3 实现创新
bazel使用教程 转
Smart Lang: VMware fixed virtual machine IP address
TS解决引入插件的类型文件不存在的问题
潇洒郎:VMware固定虚拟机IP地址
yarn(cdh)中的虚拟cpu和内存
【牛客刷题】/*C语言实现字符串左旋*/
String type function transfer problem
STL中stack和queue的使用以及模拟实现
JMeter中如何实现接口之间的关联?
华为无线设备配置智能漫游
Idea display service port --service
华为防火墙认证技术
AutoJs学习-动态解密
HCIA review and answer 2022.7.6