压缩算法

image-20240328155309320
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;

public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String s = in.next();
StringBuilder sb = new StringBuilder();
Stack<Integer> numSt = new Stack<>();
Stack<String> strSt = new Stack<>();
int num = 0;
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c <= 'Z' && c >= 'A'){
sb.append(c);
}else if(c >= '0' && c <= '9'){
num += num * 10 + (c - '0');
}else if(c == '|'){
numSt.push(num);
num = 0;
}else if(c == '['){
strSt.push(sb.toString());
sb = new StringBuilder("");
}else if(c == ']'){
StringBuilder temp = new StringBuilder(strSt.pop());
int count = numSt.pop();
while(count -- > 0){
temp.append(sb.toString());
}
sb = new StringBuilder(temp.toString());
}
}
System.out.println(sb.toString());
}
}
}

压缩算法
http://example.com/2023/10/10/算法/队列和栈/7 压缩算法/
作者
PALE13
发布于
2023年10月10日
许可协议