decoding

Encoding is the process of transforming information from one format into another. There exist several
different types of encoding scheme. In this problem we will talk about a very simple encoding technique;
Run-Length Encoding.
Run-length encoding is a very simple and easy form of data compression in which consecutive
occurrences of the same characters are replaced by a single character followed by its frequency. As an
example, the string ‘AABBBBDAA’ would be encoded to ‘A2B4D1A2’, quotes for clarity.
In this problem, we are interested in decoding strings that were encoded using the above procedure.
Input
The first line of input is an integer T (T < 50) that indicates the number of test cases. Each case is
a line consisting of an encoded string. The string will contain only digits [0-9] and letters [A-Z]. Every
inputted string will be valid. That is, every letter will be followed by 1 or more digits.
Output
For each case, output the case number followed by the decoded string. Adhere to the sample for exact
format.
You may assume the decoded string wont have a length greater than 200 and it will only consist of
upper case alphabets.
Sample Input
3
A2B4D1A2
A12
A1B1C1D1
Sample Output
Case 1: AABBBBDAA
Case 2: AAAAAAAAAAAA
Case 3: ABCD

水题

#include<cstdio>
#include<cstring>
int main(){
    int n;
    char str[105];
    scanf("%d",&n);
    for(int i = 0;i < n;i++){
        scanf("%s",str);
        int len = strlen(str);
        printf("Case %d: ",i+1);
        char tmp;
        for(int j = 0;j < len;j++){
            if(str[j] >= 'A' && str[j] <= 'Z'){
                tmp = str[j];
                int sum = 0;
                j++;
                while(str[j] >= '0' && str[j] <= '9'){
                    sum = sum*10 + str[j] - '0';
                    j++;
                }
                for(int k = 0;k < sum;k++){
                    printf("%c",tmp);
                }
                j--;
            } 
        }
        printf("\n");
    }
    return 0;
}
全部评论

相关推荐

02-28 01:18
已编辑
南昌大学 后端工程师
黑皮白袜臭脚体育生:把开源经历放个人项目上边应该更好,就像大部分人都把实习经历放个人项目上边
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务