Description
给出一个字符串,统计其中一些连续的字符c的数量k,以kc的形式输出
Input
第一行为用例组数T,每组用例占一行为一个字符串
Output
对于每组用例,输出转化后的字符串
Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C
Solution
简单字符串处理
Code
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
char s[11111];
scanf("%s",s);
int len=strlen(s);
int num=1;
char now=s[0];
len++;//长度加一便于处理最后一段连续子段
for(int i=1;i<len;i++)
{
if(s[i]==now)
{
num++;
if(i==len-1)//已经到串尾则输出
{
if(num==1)
printf("%c",now);
else
printf("%d%c",num,now);
}
}
else
{
if(num==1)
printf("%c",now);
else
printf("%d%c",num,now);
num=1;
now=s[i];
}
}
printf("\n");
}
return 0;
}