code for fun

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

描述

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.

输入

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.

输出

For each test case, output the encoded string in a line.

样例输入

2
ABC
ABBCCC

样例输出

ABC
A2B3C
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <ctype.h>
 4 #include <string.h>
 5 
 6 char a[10001];
 7 
 8 void deal(char *s){
 9     int i,j,k;
10     int L,count=0;
11     i=0;
12     L=strlen(s);
13     while(i<L){
14         j=i+1;
15         if(s[i]!=s[j]){
16             printf("%c",s[i]);
17             
18         }else{
19             k=j+1;
20             count+=2;
21             while(s[k]==s[j]){
22              k++;count++;
23             }
24             printf("%d%c",count,s[j]);
25             count=0;
26             i=k-1;
27         }
28         i++;
29     }
30     printf("\n");
31 }
32 
33 void solve(){
34   int n;
35     scanf("%d",&n);
36     getchar();
37     while(n--){
38         gets(a);
39         deal(a);
40     }    
41 }
42 
43 
44 int main(){
45     solve();
46     return 0;
47 } 
 
posted on 2013-05-22 17:39  xueda120  阅读(192)  评论(0编辑  收藏  举报