完善程序(1)(字符串解码)
“行程长度编码” (Run-Length Encoding) 是一种无损压缩算法,常 用于压缩重复字符较多的数据,以减少存储空间。假设原始字符串不包含数字字符。压缩规则如下:i) 如果原始字符串中一个字符连续出现N次 (N≥2), 在压缩字符串中它被表示为“字符+数字N”。例如,编码“A12”代表12个连续的字符A。ii) 如果原始字符串中一个字符 只出现1次,在压缩字符串中它就表示为该字符本身。例如,编码 “B” 代表1个字符 B。
以下程序实现读取压缩字符串并输出其原始的、解压后的形式。试补全程序。
#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;
}