[1020] : Encoding

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

看上去好像不难,不过如果没有理解好上面表黑色字体的内容,就很可能出错

/*
My first editing
*/

#include<stdio.h>
#include<string.h>
#define Len 10000
#define N 26

int main()
{
    int n;
    while(scanf("%d", &n)!=EOF){ // 输入测试组数n
        if(n<1 || n>100){ // 由题意,限定n的取值
            continue;
        } else {
            int i, j;
            char tag[Len];
            for(i=0; i<n; i++){
                scanf("%s", tag); // 测试内容
                int length = strlen(tag); // 输入的字符串长度
                char num[N] = {0}; // 初始化每种字符的个数为0
                for(j=0; j<length; j++){
                    switch(tag[j]){
                        case 'A' :
                            num[0]++;
                            break;
                        case 'B' :
                            num[1]++;
                            break;
                        case 'C' :
                            num[2]++;
                            break;
                        case 'D' :
                            num[3]++;
                            break;
                        case 'E' :
                            num[4]++;
                            break;
                        case 'F' :
                            num[5]++;
                            break;
                        case 'G' :
                            num[6]++;
                            break;
                        case 'H' :
                            num[7]++;
                            break;
                        case 'I' :
                            num[8]++;
                            break;
                        case 'J' :
                            num[9]++;
                            break;
                        case 'K' :
                            num[10]++;
                            break;
                        case 'L' :
                            num[11]++;
                            break;
                        case 'M' :
                            num[12]++;
                            break;
                        case 'N' :
                            num[13]++;
                            break;
                        case 'O' :
                            num[14]++;
                            break;
                        case 'P' :
                            num[15]++;
                            break;
                        case 'Q' :
                            num[16]++;
                            break;
                        case 'R' :
                            num[17]++;
                            break;
                        case 'S' :
                            num[18]++;
                            break;
                        case 'T' :
                            num[19]++;
                            break;
                        case 'U' :
                            num[20]++;
                            break;
                        case 'V' :
                            num[21]++;
                            break;
                        case 'W' :
                            num[22]++;
                            break;
                        case 'X' :
                            num[23]++;
                            break;
                        case 'Y' :
                            num[24]++;
                            break;
                        case 'Z' :
                            num[25]++;
                    }
                }
                for(i=0; i<N; i++){
                    if(num[i]>1){
                        printf("%d%c", num[i], 65+i);
                    } else if(num[i]==1){
                        printf("%c", 65+i);
                    } else {
                        continue;
                    }
                }
                printf("\n");
            }
        }
    }

    return 0;
}

不过提交后结果是:WA

在仔细看,理解题意后,尤其编译条件可知:应该是相同且连续的字符的个数,而不是说单纯计算输入的整个字符串中每种字符的个数

#include <stdio.h>  
#include <string.h>
#define Len 10000 
#define N 26 

int main()  
{  
    int n;
    while(scanf("%d", &n)!=EOF){ //测试组数  
        int i, j, k; 
        char a[Len], b[N]; // 数组a用来存储测试内容,数组b用来存储a的简化形式(去掉连续重复的,只留下一个作为代表) 
        int c[N] = {0}; // 26个字母的个数每种都初始化为0 
        for(i = 0; i < n; i ++){  
            scanf("%s", a); // 输入测试内容 
            int len = strlen(a); 

            b[0] = a[0]; 
            for(j = 0, k = 0; j < len; j ++){  
                if(b[k] == a[j]){  
                c[k]++;  
                }  else  {  
                b[k+1] = a[j];  
                k++;  
                c[k]++;  
                }  
            } 
        } 

        for(j = 0; j <= k; j ++)  
        {  
            if(c[j] != 1)  
                printf("%d%c", c[j], b[j]);  
            else  
                printf("%c", b[j]);  
        }  
        printf("\n");  

    }  
    return 0;  
}  

这里写图片描述

这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值