Skip to content

Commit 6e10f09

Browse files
committed
2 parents d6eb8c5 + 74cc428 commit 6e10f09

11 files changed

+263
-259
lines changed

arithmetic_decode.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
ll len;
67+
string code;
68+
69+
cin >> len;
70+
for(int i=0; i<=256; i++) cin >> lower[i];
71+
cin >> code;
72+
73+
string decode = "";
74+
75+
ll _low[len];
76+
ll _high[len];
77+
78+
_low[0] = MIN;
79+
_high[0] = MAX;
80+
81+
ll value = to_int(0, code);
82+
int count_taken = 16;
83+
bool flag = 0;
84+
85+
for(int i=1;i<len;i++){
86+
ll range = (_high[i-1] - _low[i-1] + 1);
87+
ll nxt = (((value - _low[i-1]) + 1) * len - 1) / range;
88+
89+
int symbol;
90+
for(symbol = 1; lower[symbol] <= nxt; symbol++);
91+
92+
_low[i] = _low[i-1] + (range * lower[symbol-1]) / len;
93+
_high[i] = _low[i-1] + (range * lower[symbol]) / len - 1;
94+
95+
if(symbol == 256)
96+
break;
97+
98+
decode += char(symbol);
99+
100+
for(;;){
101+
if(_high[i] >= HALF){
102+
if(_low[i] >= HALF){
103+
value -= HALF;
104+
_low[i] -= HALF;
105+
_high[i] -= HALF;
106+
}
107+
else if (_low[i] >= ONE_QTR && _high[i] < THREE_QTR)
108+
{
109+
value -= ONE_QTR;
110+
_low[i] -= ONE_QTR;
111+
_high[i] -= ONE_QTR;
112+
}
113+
else
114+
{
115+
break;
116+
}
117+
}
118+
_low[i] = 2 * _low[i];
119+
_high[i] = 2 * _high[i] + 1;
120+
value = add_bit(2 * value, count_taken, flag, code);
121+
count_taken++;
122+
}
123+
}
124+
125+
cout << decode << endl;
126+
127+
}

arithmetic_encode.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)