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;
}