EncodingTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54227 Accepted Submission(s): 24112 http://acm.hdu.edu.cn/showproblem.php?pid=1020 Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following method: 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 |
代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
int n;
char s[10005];
cin>>n;
while(n--)
{
cin>>s;
int n=strlen(s);
int h=1;
for(int i=1;i<=n;i++)
{
if(s[i]==s[i-1])
{
h++;
}
else
{
if(h==1)
cout<<s[i-1];
else if(h>1)
cout<<h<<s[i-1];
h=1;
}
}
cout<<endl;
}
return 0;
}