Problem B: Encoding
Time Limit: 1 Sec Memory Limit: 32 MB
Submit: 527 Solved: 247
[Submit] [Status] [Web Board] [Creator:Imported]
Description
Given a string containing only ‘A’ - ‘Z’, we could encode it using the following method:
-
Each sub-string containing k same characters should be encoded to “kX” where “X” is the only character in this sub-string.
-
If the length of the sub-string is 1, ‘1’ should be ignored.
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 Copy
2
ABC
ABBCCCAA
Sample Output Copy
ABC
A2B3C2A
一定要注意判断c的个数后加else;否则输入如图,还有每次判断完输出后要重新置1
int main() {
int n;
cin>>n;
int c=1;
getchar();
string s;
string a;
while(n--){
getline(cin,s);
// cout<<s;
for(int i=0;i<s.length();i++){
if(s[i]==s[i+1]){
c++;
}
else{
if(c>1)
{
cout<<c<<s[i];
c=1;
}else if(c<=1)
{
cout<<s[i];
c=1;
}
}
}
cout<<endl;
}
return 0;
}