HDU 1020 Encoding

来源:互联网 发布:怎样建淘宝团购微信群 编辑:程序博客网 时间:2024/06/05 21:11
Encoding
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 

分析

简单的暴力,直接遍历一边输出结果即可,注意cnt==1时的特殊输出。

 

AC代码如下:

#include <cstdio>#include <cstring>using namespace std;int main(){    int t,i,cnt;    char ch[10005];    scanf("%d",&t);    while(t--)    {        scanf("%s",ch);        for(i = 0; ch[i]!='\0';)        {            cnt = 1;            while(ch[i] == ch[i+1])              //计算重复次数            {                cnt++;                i++;            }            if(cnt == 1) printf("%c",ch[i]);     //等于1的时候特殊处理            else printf("%d%c",cnt,ch[i]);            i++;        }        printf("\n");    }    return 0;}


 

0 0