HDU1020 Encoding

 

 

 1 //2016-07-13
 2 
 3 #include "iostream"
 4 #include "cstdio"
 5 #include "vector"
 6 #include "string"
 7 using namespace std;
 8 int main()
 9 {
10 int t;
11 scanf("%d",&t);
12 string a;
13 while(t--){
14 cin>>a;
15 int len=a.length();
16 int count01=1;
17 vector<char> v;
18 int i;
19 for( i=1;i<len;i++){
20 if(a.at(i)==a.at(i-1)){//此位置字母與前一個是否相同
21 count01++;
22 continue;//相同計數器加1
23 }else{//不相同判斷計數器是否爲1(由於計數器爲一時不插入字符串)
24 if(count01!=1){//計數器可能很是大,接近10000,存入一字符數組,在倒序插入字符串
25 char str[10000];
26 int j=0;
27 while(count01!=0){
28 str[j]=count01%10+48;
29 j++;
30 count01/=10;
31 }
32 for(int k=j-1;k>=0;k--){
33 v.push_back(str[k]);
34 }
35 v.push_back(a.at(i-1));//插入前一字符
36 count01=1;
37 }
38 else{
39 v.push_back(a.at(i-1));//計數器爲一,直接插入前一字符
40 }
41 
42 }
43 
44 }
45 if(count01!=1){//
46 char str[10000];
47 int j=0;
48 while(count01!=0){
49 str[j]=count01%10+48;
50 j++;
51 count01/=10;
52 }
53 for(int k=j-1;k>=0;k--){
54 v.push_back(str[k]);
55 }
56 }
57 v.push_back(a.at(i-1));//由於老是拿此位置與前一個位置的字符比較,而輸出前一個字符,
58 //輸入字符串沒有後一個字符串與其相比而輸出最後一個字符,因此對最後一個字符單獨處理
59 
60 vector<char>::iterator it;//迭代器遍歷輸出
61 for(it=v.begin();it!=v.end();it++){
62 printf("%c",*it);
63 }
64 printf("\n");
65 }
66 }
相關文章
相關標籤/搜索