Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method:
1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
2. If the length of the sub-string is 1, '1' should be ignored.
1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.
2. 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
解题报告:字符串处理题
View Code
1 #include<stdio.h> 2 #include<string.h> 3 char str[10005]; 4 int num[30]; 5 int main() { 6 int n; 7 scanf("%d",&n); 8 while(n--) { 9 scanf("%s",str); 10 int len=strlen(str); 11 int sum=1; 12 for(int i=0;i<len;++i) 13 if(str[i]==str[i+1]) 14 sum++; 15 else { 16 if(sum==1) 17 printf("%c",str[i]); 18 else 19 printf("%d%c",sum,str[i]); 20 sum=1; 21 } 22 str[0]='\0'; 23 printf("\n"); 24 } 25 return 0; 26 }