water- Encoding
Encoding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18521 Accepted Submission(s): 7973
Problem Description
Given a string containing 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.
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.
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
**********************************************************************************************************
简单题,统计字母的个数。
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 char str[10005]; 5 using namespace std; 6 int main() 7 { 8 int n,cnt,len; 9 char c; 10 scanf("%d",&n); 11 getchar(); 12 while(n--) 13 { 14 cnt=1; 15 fgets(str,sizeof(str),stdin); 16 c=str[0]; 17 len=strlen(str); 18 for(int i=1;i<=len;i++) 19 { 20 if(c==str[i]) 21 cnt++; 22 else 23 { 24 if(cnt==1) 25 printf("%c",str[i-1]); 26 else 27 printf("%d%c",cnt,str[i-1]); 28 c=str[i]; 29 cnt=1; 30 } 31 } 32 } 33 return 0; 34 }
Do one thing , and do it well !