编程介的小学生 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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现