字符串解码
“行程长度编码”(Run-Length Encoding)是一种无损压缩算法,常用于压缩重复字符较多的数据,以减少存储空间。假设原始字符串不包含数字字符,压缩规则如下:
● i) 如果原始字符串中一个字符连续出现N次(N≥2N ),在压缩字符串中它被表示为“字符+数字N”。例 如,编码“A12”代表12个连续的字符A。
● ii) 如果原始字符串中一个字符只出现1次,在压缩字符串中表示为该字符本身。例如,编码“B”代表1 个字符B。
以下程序实现读取压缩字符串并输出其原始的、解压后的形式,试补全程序。
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 | #include <cctype> #include <iostream> #include <string> using namespace std; int main() { string z; cin >> z; string s = ""; for (int i = 0; i < z.length(); ) { char ch = z[i]; if (① && isdigit(z[i + 1])) { i++; int count = 0; while (i < z.length() && isdigit(z[i])) { count = ②; i++; } for (int j = 0; j < ③; ++j) { s += ch; } } else { s += ④; ⑤; } } cout << s << endl; return 0; } |
0 of 5 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 5 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
①处应填( )
②处应填( )
③处应填( )
④处应填( )
⑤处应填( )
