hdoj 1020 Encoding

来源:互联网 发布:因笑谓迈曰 汝识之乎 编辑:程序博客网 时间:2024/05/29 09:15

要注意没有说要输出某字符总的个数;

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>char s[10001];int main(){int t, c;scanf( "%d", &t );getchar();while( t -- ){scanf( "%s", s );int l = strlen(s);c = 1;for( int i = 1; i < l; i ++ ){if( s[i]==s[i-1] ) ++c;else{if(c>1)printf( "%d", c );printf( "%c", s[i-1] );c = 1;}}if( s[l-1] == s[l-2] )printf( "%d%c", c, s[l-2] );elseprintf( "%c", s[l-1] );printf( "\n" );} }



0 0
原创粉丝点击