Encoding

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.

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

 1 #include <stdio.h>
 2 #include <string.h>
 3  
 4 int main(){
 5     int N;
 6     char A_Z[10001];
 7     int i;
 8     int length;
 9     char flag;
10     int amount;
11      
12     scanf("%d",&N);
13      
14     while(N--){
15         scanf("%s",A_Z);
16         length=strlen(A_Z);
17          
18         flag=A_Z[0];
19         amount=1;
20         for(i=1;i<length;i++){  
21             if(flag==A_Z[i]){
22                 amount++;
23                  
24                 if(i==length-1){
25                     if(amount==1)
26                         printf("%c",flag);
27                      
28                     else
29                         printf("%d%c",amount,flag);
30                 }
31             }
32                  
33             else{
34                 if(amount==1)
35                     printf("%c",flag);
36                      
37                 else
38                     printf("%d%c",amount,flag);
39                      
40                 if(i==length-1)
41                     printf("%c",A_Z[i]);
42                      
43                 amount=1;
44                 flag=A_Z[i];
45             }
46         }
47          
48         printf("
");
49          
50     }
51     return 0;
52 }
原文地址:https://www.cnblogs.com/zqxLonely/p/4054502.html