题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805262018265088

压缩的时候用了个trick,在最后一位加入了一个不可能字符

代码

  1. #include<string>
  2. #include<iostream>
  3. using namespace std;
  4. int main(){
  5. string str;
  6. char c;
  7. scanf("%c", &c);
  8. getchar();
  9. getline(cin, str);
  10. if(c == 'C'){
  11. str += str[str.size() - 1] + 1;
  12. int i = 0, j = 0;
  13. while( i != str.size() - 1){
  14. if(str[j] == str[i] && j < str.size() - 1){
  15. j++;
  16. } else {
  17. if(j - i == 1) printf("%c", str[i]);
  18. else printf("%d%c", j - i, str[i]);
  19. i = j;
  20. }
  21. }
  22. }
  23. else if(c == 'D'){
  24. int i = 0;
  25. while(i < str.size()){
  26. if(str[i] <= '9' && str[i] >= '0'){
  27. int count = 0;
  28. while(str[i] <= '9' && str[i] >= '0'){
  29. count *= 10;
  30. count += str[i] - '0';
  31. i++;
  32. }
  33. for(int j = 0; j < count; j++){
  34. printf("%c", str[i]);
  35. }
  36. i++;
  37. } else {
  38. printf("%c", str[i]);
  39. i++;
  40. }
  41. }
  42. }
  43. return 0;
  44. }