0%

[題解]跑長編碼與資料壓縮

a109. 跑長編碼與資料壓縮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
#define ld long double
using namespace std;

int main(){
int n,origin,after,times;
string s,t,binary[8]={"000","001","010","011","100","101","110","111"};
char ch;
cin>>n;
cin.ignore();
while(n--){
getline(cin,s);
t="";
origin = s.size();
after = 0;
ch = s[0];
times = 1;
int len = s.size();
for(int i=1;i<=len;i++){
if(i!=len && s[i]!='0'&&s[i]!='1'){
after = -1;
break;
}
if(s[i]!=ch){
t+=ch;
t+=binary[times];
t+=" ";
times=1;
ch = s[i];
after+=4;
}
else{
times++;
if(times==7){
t+=ch;
t+=binary[7];
t+=" ";
times=1;
after +=4;
ch = s[i+1];
i++;
}
}
}
if(after==-1)cout<<after<<endl;
else cout<<t<<fixed<<setprecision(0)<<(double)after*100/origin<<"%"<<endl;
}
}