1020 Encoding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45655    Accepted Submission(s): 20188

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.
 

 

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
 
 
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     int t;
 7     cin>>t;
 8     while(t--)
 9     {
10         string s0,s1;
11         cin>>s0;
12         int count;
13         for(int i=0;i<s0.length();i++)
14         {
15             count=0;
16             for(int j=i;j<=s0.length();j++)
17             {
18                 if(j==s0.length())
19                 {
20                     if(count==1)
21                     s1+=s0[i];
22                     else
23                     {
24                             string s2;
25                             while(count>0)
26                             {
27                                 s2+=count%10+'0';
28                                 count/=10;
29                             }
30                             for(int i=s2.length()-1;i>=0;i--)
31                             s1+=s2[i];
32                             s1+=s0[i];
33                     }
34                     i=j-1;
35                     break;
36                 }
37                 if(s0[i]==s0[j])
38                 {
39                     count++;
40                 }
41                 else
42                 {
43                     if(count==1)
44                     s1+=s0[i];
45                     else
46                     {
47                             string s2;
48                             while(count>0)
49                             {
50                                 s2+=count%10+'0';
51                                 count/=10;
52                             }
53                             for(int i=s2.length()-1;i>=0;i--)
54                             s1+=s2[i];
55                             s1+=s0[i];
56                     }
57                     i=j-1;
58                     break;
59                 }
60             }
61         }
62         cout<<s1<<endl;
63     }
64     return 0;
65  } 

总是感觉代码写得复杂了,不过小白就不要求这么多了,AC就好

posted @ 2017-05-08 00:32  小花同学  阅读(255)  评论(0编辑  收藏  举报