Encoding

来源:互联网 发布:数据库范式的算法例题 编辑:程序博客网 时间:2024/05/11 03:16

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

2ABCABBCCC
              

Sample Output

 ABCA2B3C 

 

之前比赛时做过这个题,但是因为对这种题不熟练,所以没有做,先在尝试做了这道题,感觉只是边尝试边做的,虽然一次做对了,但是感觉明显自己的代码很不成熟!

#include<stdio.h>#include<string.h>int main(){    int i,j,m,n,s;    char str[11000];    scanf("%d\n",&m);    while(m--)    {        gets(str);        n=strlen(str);        int p=0;        for(i=0;i<n;)        {            s=0;            for(j=i;j<n;j++)            {                if(str[i]==str[j])                    s++;                else break;            }            if(s==1) printf("%c",str[i]);            else            printf("%d%c",s,str[i]);            i=i+s;        }        printf("\n");    }    return 0;}


 

 

0 0