Encoding编码 18

Encoding编码

Problem Description

Given a stringcontaining 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.

 

由于仅包含“A”的字符串 - 'Z',我们可以用下面的方法对其进行编码:

 

包含k个相同的字符1.各子串应当被编码为“的kX”,其中“X”是在该子串的唯一的字符。

 

2.如果子串的长度为1,'1'应该被忽略。

Input

The first linecontains an integer N (1 <= N <= 100) which indicates the number of testcases. The next N lines contain N strings. Each string consists of only 'A' -'Z' and the length is less than 10000.

 第一行包含一个整数N(1<= N<=100)表示测试用例的数量。接下来的N行包含N个字符串。每个字符串仅由'A' - 'Z'和长度小于10000。

 

Output

For each testcase, output the encoded string in a line.

 对于每个测试用例,输出线路编码字符串

 

Sample Input

2

ABC

ABBCCC

 

 

Sample Output

ABC

A2B3C


代码如下:

#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//	const int MAXN=10005;
	char a[10005];
int main()
{
    int i,temp;
    int T;
    scanf("%d",&T);//两组测试数据 
    while(T--)
    {
        scanf("%s",&a);//输入的是字符串 
        i=0;
        while(a[i]!='\0')//这是判断字符串结束的标志 
        {
            temp=i;
            while(a[temp+1]==a[i])
            {
                temp++;
            }    
            if(temp>i)
			printf("%d",temp-i+1);
            printf("%c",a[i]);
            i=temp;
            i++;
        } 
        printf("\n");   
    } 
    return 0;   
}    


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值