编程介的小学生 2019-03-02 23:59 采纳率: 20.5%
浏览 584

求问一个英文字母的编码的问题,采用C语言如何才能实现的呢

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

  • 写回答

1条回答

  • pickaBoo 2019-03-03 11:56
    关注
    #include<stdio.h>
    #include<string.h>
    char str[10001];
    char result[10001];
    int main()
    {
        int numTest,i,j,k;
        int strLen;
        int resIdx;
        scanf("%d",&numTest );
        while(numTest--)    {
            scanf("%s",str);
            strLen = strlen(str);
            resIdx = 0;                                  //initialize the result index for each test Case
            for(i=0; i<strLen; i++)     {
                for(j=i, k=0; j<strLen ; k++, j++)          {
                    if(str[i]!=str[j])
                    break;
                }
                if(k>1) {
                   result[resIdx++]='0'+k;   //convert the number into char
                }
                result[resIdx++]=str[i];       // copy the original char
                i=j-1;                                   //move the i to the point before next different point
            }                                            // the i++ in the outer loop will move i to next differnt point
            result[resIdx]=0;                  // null the end of each result, since it maybe shorter than last result
            printf("%s\n",result);
        }
        return 0;
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘