压缩
作者:互联网
import java.util.Scanner;
import java.util.Stack;
public class lyw03 {
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);
}
}
标签:ch,Scanner,压缩,Stack,poll,stack,String 来源: https://www.cnblogs.com/saowei/p/16620087.html