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 }

 

posted @ 2016-07-13 14:40  kimsimple  阅读(218)  评论(0编辑  收藏  举报