hdu_1020_Encoding_201310172120

Encoding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22110    Accepted Submission(s): 9687

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 char str[10100];
 5 
 6 int main()
 7 {
 8     int cases;
 9     scanf("%d",&cases);
10     getchar();
11     while(cases--)
12     {
13         int i,len,count=0;
14         char c;
15         scanf("%s",str);
16         len=strlen(str);
17         i=0;c=str[i];
18         while(i<len)
19         {            
20             while(str[i+1] == c)
21             {
22                 count++;
23                 i++;
24             }
25             if(count)
26             {
27                 printf("%d%c",count+1,c);
28                 count = 0;
29                 c = str[i+1];
30             }
31             else
32             {
33                 printf("%c",c);
34                 count = 0;
35                 c = str[i+1];
36             }
37             i++;
38         }
39         printf("
");
40     }
41     return 0;
42 }

做过,水过!

 
原文地址:https://www.cnblogs.com/xl1027515989/p/3375038.html