70019 - 2025CSP-J 完善程序1
统计题目(材料题)
(1)字符串解码
“行程长度编码”(Run-Length Encoding)是一种无损压缩算法,常用于压缩重复字符较多的数据,以减少存储空间。假设原始字符串不包含数字字符,压缩规则如下:
① 如果原始字符串中一个字符连续出现N次$(N>2)$,在压缩字符串中表示为“字符+数字N”。例如,编码”A12”代表12个连续的字符A。
② 如果原始字符串中一个字符只出现1次,在压缩字符串中表示为该字符本身。例如,编码“B"代表1个字符B。
以下程序实现读取压缩字符串并输出其原始的、解压后的形式,试补全程序。
1 #include <cctype>
2 #include <iostream>
3 #include <string>
4 using namespace std;
5
6 int main() {
7 string z;
8 cin >> z;
9 string s = "";
10 for (int i = 0; ①; )f
11 char ch = z[i];
12 if (isdigit(z[i + 1])) {
13 int count = 0;
14 i++;
15 while (i < z.length() && isdigit(z[i])) {
16 count = ②;
17 i++;
18 for (int j = 0; j < ③; ++j) {
19 S += ch;
20 } else {
21 S += ④;
22 ⑤;
23 }
24 }
25 }
26 cout << s << end;
27 return 0;
28 }

关注我们