1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ #define ll long long
6
+
7
+ const ll MIN = 0 ;
8
+ const ll MAX = 65535 ;
9
+
10
+ int lower[257 ];
11
+
12
+ #define err () cout<<" ==================================" <<endl;
13
+ #define errA (A ) for (auto i:A) cout<<i<<" " ;cout<<endl;
14
+ #define err1 (a ) cout<<#a<<" " <<a<<endl
15
+ #define err2 (a,b ) cout<<#a<<" " <<a<<" " <<#b<<" " <<b<<endl
16
+ #define err3 (a,b,c ) cout<<#a<<" " <<a<<" " <<#b<<" " <<b<<" " <<#c<<" " <<c<<endl
17
+ #define err4 (a,b,c,d ) cout<<#a<<" " <<a<<" " <<#b<<" " <<b<<" " <<#c<<" " <<c<<" " <<#d<<" " <<d<<endl
18
+
19
+ string write_bits (bool bit, int bit_to_fall){
20
+ string tmp;
21
+ tmp += to_string (bit);
22
+ while (bit_to_fall){
23
+ tmp += to_string (!bit);
24
+ bit_to_fall -= 1 ;
25
+ }
26
+ return tmp;
27
+ }
28
+
29
+ int to_int (int _pos, string &encode){
30
+ int n = 0 ;
31
+ for (int i = _pos; i < sizeof (short ) * 8 + _pos; i++)
32
+ {
33
+ n <<= 1 ;
34
+ n |= encode[i] - ' 0' ;
35
+ }
36
+ return n;
37
+ }
38
+
39
+ ll add_bit (ll value, int count_taken, bool &flag, string &encode){
40
+ bitset<16 > a (value);
41
+
42
+ if (flag == 1 ){
43
+ a.reset (0 );
44
+ }
45
+ else if (count_taken > encode.length ()){
46
+ a.set (0 );
47
+ flag = 1 ;
48
+ }
49
+ else if (encode[count_taken] == ' 1' ){
50
+ a.set (0 );
51
+ }
52
+ else if (encode[count_taken] == ' 0' ){
53
+ a.reset (0 );
54
+ }
55
+
56
+ value = (ll)(a.to_ulong ());
57
+ return value;
58
+ }
59
+
60
+ int main (){
61
+
62
+ const ll ONE_QTR = MAX / 4 + 1 ;
63
+ const ll HALF = 2 * ONE_QTR;
64
+ const ll THREE_QTR = 3 * ONE_QTR;
65
+
66
+ string text;
67
+ cin >> text;
68
+
69
+ int len = text.length () + 1 ;
70
+ ll low[len];
71
+ ll high[len];
72
+ low[0 ] = MIN;
73
+ high[0 ] = MAX;
74
+ int current = 1 ;
75
+ int i = 0 ;
76
+
77
+ for (auto i: text){
78
+ lower[i] += 1 ;
79
+ }lower[256 ] = 1 ;
80
+
81
+ for (int i=1 ; i<=256 ; i++){
82
+ lower[i] += lower[i-1 ];
83
+ }
84
+
85
+ int bit_to_fall = 0 ;
86
+ string code = " " ;
87
+
88
+ while (i < len){
89
+ if (i == len-1 ) current = 256 ;
90
+ else current = text[i];
91
+ i += 1 ;
92
+
93
+ ll range = high[i-1 ] - low[i-1 ] + 1 ;
94
+ low[i] = low[i-1 ] + (range * lower[current-1 ]) / len;
95
+ high[i] = low[i-1 ] + (range * lower[current]) / len - 1 ;
96
+
97
+ while (true ){
98
+ if (high[i] < HALF){
99
+ code += write_bits (0 , bit_to_fall);
100
+ bit_to_fall = 0 ;
101
+ }
102
+ else if (low[i] >= HALF){
103
+ code += write_bits (1 , bit_to_fall);
104
+ bit_to_fall = 0 ;
105
+ low[i] -= HALF;
106
+ high[i] -= HALF;
107
+ }
108
+ else if (low[i] >= ONE_QTR and high[i] < THREE_QTR){
109
+ bit_to_fall += 1 ;
110
+ low[i] -= ONE_QTR;
111
+ high[i] -= ONE_QTR;
112
+ }
113
+ else
114
+ break ;
115
+ low[i] = 2 * low[i];
116
+ high[i] = 2 * high[i] + 1 ;
117
+ }
118
+ }
119
+
120
+ ll p = low[i-1 ];
121
+
122
+ for (int j=15 ; j>=0 ; j--){
123
+ if ((p & (1LL <<j))) code += ' 1' ;
124
+ else code += ' 0' ;
125
+ }
126
+
127
+ cout << len << endl;
128
+
129
+ for (int i=0 ; i<=256 ; i++)
130
+ cout << lower[i] << endl;
131
+
132
+ cout << code << endl;
133
+
134
+ }
135
+
136
+ // qhnwnkuewhsqmgbbuqcljjivswmdkqtbxixmvtrrbljptnsnfwzqfjmafadrrwsofsbcnuvqhffbsaqxwpqcaceh
0 commit comments