fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. void rr(char *s) {
  6. if (s[0] == '\0') {
  7. return;
  8. }
  9.  
  10. char c[1000]; // Assuming the output will not exceed 1000 characters.
  11. int cnt = 1, j = 0;
  12.  
  13. for (int i = 1; i < strlen(s); i++) {
  14. if (s[i] == s[i - 1]) {
  15. cnt++;
  16. } else {
  17. c[j++] = s[i - 1];
  18. if (cnt > 1) {
  19. sprintf(c + j, "%d", cnt);
  20. while (isdigit(c[j])) j++;
  21. }
  22. cnt = 1;
  23. }
  24. }
  25.  
  26. // Adding the last character and its count
  27. c[j++] = s[strlen(s) - 1];
  28. if (cnt > 1) {
  29. sprintf(c + j, "%d", cnt);
  30. while (isdigit(c[j])) j++;
  31. }
  32. c[j] = '\0'; // Null-terminate the new string.
  33.  
  34. // Remove '1' after letters
  35. char fc[1000];
  36. j = 0;
  37. for (int i = 0; i < strlen(c); i++) {
  38. if (isalpha(c[i]) && c[i + 1] == '1') {
  39. fc[j++] = c[i];
  40. i++; // Skip the '1'
  41. } else {
  42. fc[j++] = c[i];
  43. }
  44. }
  45. fc[j] = '\0';
  46.  
  47. // If the final compressed string is shorter, print it
  48. if (strlen(fc) < strlen(s)) {
  49. printf("%s\n", fc);
  50. return;
  51. }
  52.  
  53. // Otherwise, print the original string
  54. printf("%s\n", s);
  55. }
  56.  
  57. int main() {
  58. char input[1000];
  59. scanf("%s", input);
  60. rr(input);
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0.01s 5284KB
stdin
QQQQQQQQQQQQQQQABCCCCCCCCCCZZZZZZZZZZ
stdout
Q5ABC0Z0