Life is short, so we need program

每日一题, 积累从点滴开始

  :: 首页 :: 博问 :: 闪存 :: :: 联系 :: :: 管理 ::

Encoding

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


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 char s[10001];
 4 
 5 int main(){
 6     int n, i;
 7     scanf("%d", &n);
 8     while(n--){
 9         scanf("%s", &s);
10         int len = strlen(s);
11         char tmp = s[0];
12         int k=1;
13         for(i=1; i<len+1; i++){
14             if(tmp == s[i]){
15                 k++;  
16             }else{
17                 if(k!=1)
18                     printf("%d", k);
19                 printf("%c", tmp);
20                 tmp = s[i];
21                 k=1;
22             }
23         }
24         printf("\n");
25     }
26 }

 

 
posted on 2012-06-12 20:02  CDU_ICPC  阅读(222)  评论(0编辑  收藏  举报