(TOJ1248)Encoding

描述

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 } 
 
作者:xueda120
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/xueda120/p/3093197.html