0%

C++ 彰雲嘉區複賽 106-2 跑長編碼與資料壓縮

題目連結:e522: 106 彰雲嘉區複賽 - Q2 跑長編碼與資料壓縮

字串基本應用

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
50
51
52
53
54
55
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

string num[10]={"000","001","010","011","100","101","110","111"};

string print(int sum,char bit){
string re(1,bit);
re+=num[sum];
re+=" ";
return re;
}

int main(){
ios::sync_with_stdio(0);cin.tie(0);

int iptNum=0;
cin>>iptNum;

string bit,out;
getline(cin,bit);

while(iptNum--){
out="";
getline(cin,bit);
int j,sum=1;
long long rate=0;
for(j=1;j<bit.size();j++){
if(bit[j]!='0' && bit[j]!='1'){
cout<<"-1\n";
j++;
break;
}

if(bit[j-1]==bit[j] && sum<7){
sum++;
}else{
out+=print(sum,bit[j-1]);
rate+=4;
sum=1;
}
}
out+=print(sum,bit[j-1]);
rate+=4;
rate*=1000;
rate/=bit.size();
rate=(rate+5)/10;

if(bit[j-1]=='0' || bit[j-1]=='1'){
cout<<out<<rate<<"\n";
}
}
return 0;
}