[解题报告]HDU 1020 Encoding

Encoding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19071    Accepted Submission(s): 8230


Problem 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
2 ABC ABBCCC
 

 

Sample Output
ABC A2B3C
 

 

Author
ZHANG Zheng
 

 

Recommend
JGShining
 
 
 
水题,详细过程见注释
#include<stdio.h>
#include<string.h>
char str[10005];
int main()
{
    int n;
    int i;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",str);
        int len=strlen(str);//记录字符长度
        str[len]='/0';//字符串收尾
        int cnt=1;
        char ch=str[0];
        for(i=1;i<=len;i++)
        {
            if(ch==str[i])//把第一个字母和后面所有的比较,有重复就用cnt记录
            {
                cnt++;
            }
            else
            {
                if(cnt!=1) printf("%d",cnt);//如果有复数个,打出个数,只有一个则不打出
                putchar(ch);//无论有几个,打出对应字母
                ch=str[i];//要比较的字母后移
                cnt=1;//初始化cnt
            }
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2013-03-18 21:00  三人木君  阅读(296)  评论(0编辑  收藏  举报