我的个人网站 Cheese的个人主页http://www.cheese.ren/
博客来源 PAT 乙级 1078 字符串压缩与解压-Cheese的个人博客PAT 乙级 1078 字符串压缩与解压 C++http://blog.cheese.ren/98
欢迎交换友链 :-)
#include <bits/stdc++.h>
using namespace std;
int main() {
char type;
cin >> type;
string str;
getchar();
getline(cin, str);
// compress
if (type == 'C') {
int c_count=1;
char c_temp=str[0];
for (int i=1; i<str.length()+1; i++) {
// pre char is different from this one
if (str[i] != c_temp) {
if (c_count != 1)
printf("%d", c_count);
printf("%c", c_temp);
c_temp = str[i];
c_count = 1;
}
// pre char equal this one
else if (str[i] == c_temp)
c_count++;
}
}
// depress
else if (type == 'D') {
int repeat_num=1;
for (int i=0; i<str.length(); i++) {
// if char is number
if (str[i] >= '0' && str[i] <= '9') {
if (str[i-1] >= '0' && str[i-1] <= '9' && i != 0) {
repeat_num *= 10;
repeat_num += str[i] - '0';
}
else
repeat_num = str[i] - '0';
}
// if char is letter
else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == ' ') {
while (repeat_num--)
printf("%c", str[i]);
repeat_num = 1;
}
}
}
printf("\n");
return 0;
}