版权声明:如有帮助,赞一个可好。邮箱:[email protected] https://blog.csdn.net/qq_40946921/article/details/89502926
Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method:
-
Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
-
If the length of the sub-string is 1, '1' should be ignored.
Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.
Output
For each test case, output the encoded string in a line.
Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C
#include<stdio.h>
#include<string.h>
int main() {
int n,count,len;
char str[10001];
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%s", &str);
len = strlen(str);
for (int j = 0; j < len; ++j) {
count = 1;
while (j<len&&str[j] == str[j + 1] && ++j)
count++;
if (count > 1)printf("%d", count);
printf("%c", str[j]);
}
printf("\n");
}
return 0;
}