Encoding HDU--1020

简单字符串处理:

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.

(提示:输入 AABAC 则 输出:2ABAC)

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 void doProcess(string &str)
 6 {
 7     int count,i,j;
 8     for(i=0;i<str.length();)
 9     {
10         count=1;
11         for(j=i+1;j<str.length();++j)
12         {
13             if(str[i]==str[j])
14                 count++;
15             else break;
16         }
17         if(count>1)
18         {
19             cout<<count<<str[i];
20         }
21         else cout<<str[i];
22         i=j;   //继续处理下一种字符
23     }
24     cout<<endl;
25 }
26 
27 int main()
28 {
29     int caseNum,i;
30     string strTemp;
31     while(cin>>caseNum)
32     {
33         for(i=0;i<caseNum;i++)
34         {
35             cin>>strTemp;
36             doProcess(strTemp);
37         }
38     }
39 return 0;
40 }

 

posted @ 2014-11-06 15:40  zhoudan  阅读(132)  评论(0)    收藏  举报