solution
- 输入的字符串含空格,可以先吸收换行符再用getline()
- 解压时,字符数可能不止是各位数字,存在多位数的情况
- 压缩时,别漏了最后一组字符
#include<iostream>
#include<string>
using namespace std;
int main(){char now, com;int cnt = 1, j, power;string s;cin >> com;getchar();getline(cin, s);if(com == 'C'){now = s[0];for(int i = 1; i < s.size(); i++){if(s[i] == now) cnt++;else{if(cnt != 1) cout << cnt;cout << now;now = s[i];cnt = 1;}}if(cnt != 1) cout << cnt;cout << now;}else{for(int i = 0; i < s.size(); i++){if(isdigit(s[i])){cnt = 0;for(j = i; j < s.size() && isdigit(s[j]); j++){cnt = s[j] - '0' + cnt * 10;}for(int k = 0; k < cnt; k++){cout << s[j];}i = j;}else cout << s[i];}}return 0;
}