Problem Description
Givena string containing only 'A' - 'Z', we could encode it using the followingmethod:
1.Each sub-string containing k same characters should be encoded to"kX" where "X" is the only character in this sub-string.
2. Ifthe length of the sub-string is 1, '1' should be ignored.
Input
Thefirst line contains an integer N (1 <= N <= 100) which indicates thenumber of test cases. The next N lines contain N strings. Each string consistsof only 'A' - 'Z' and the length is less than 10000.
Output
Foreach test case, output the encoded string in a line.
Sample Input
2
ABC
ABBCCC
Sample Output
ABC
A2B3C
参考代码:
#include<iostream>
using namespace std;
int main(){
string s;
int i,count;
char c;
cin>>s;
c=s[0];
count=1;
for(i=1;i<s.length();i++){
if(c==s[i]){
count++;
}else{
if(count>1)
cout<<count;
cout<<c;
c=s[i];
count=1;
}
}
if(count>1)
cout<<count;
cout<<c<<endl;
return 0;
}
测试结果: