-
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
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
#include "common.h"
#include "huffman.h"
#define pb push_back
struct weightstruct
{
int word;
int clause;
int sentence;
weightstruct(int a=0,int b=0,int c=0):word(a),clause(b),sentence(c){}
weightstruct operator + (const weightstruct& w) const
{
return weightstruct(word+w.word,clause+w.clause,sentence+w.sentence);
}
};
weightstruct getweight(string s)
{
if(s[0]=='$')
return weightstruct(200,100,100);
if(s=="one"||s=="two"||s=="three"||s=="four"||s=="five"||s=="six"||s=="seven"||s=="eight"||s=="nine")return weightstruct(100,50,50);
if(s=="example")return weightstruct(-300,-200,-200);
if(s=="a"||s=="is"||s=="are"||s=="to"||s=="so"||s=="as") return weightstruct(-200,0,0);
if(s=="the")
return weightstruct(-200,0,0);
/*
if(s.length()==1)
return weightstruct(-300,0,0);
if(s.length()==2)
return weightstruct(-50,0,0);
*/
return weightstruct(-10,0,0);
}
void preprocessword(int stage,string& s,string& next,int pos)
{
if(stage==-1)//after deleting low weights
{
if(s[0]=='$')
s=s.substr(1,s.length()-2);
}
if(stage==0)
{
string tmp;
for(char c:s)
tmp+=('A'<=c&&c<='Z')?(c-'A'+'a'):(c);
s=tmp;
}
if(stage==1)
{
if(s=="and")
s=",";
if(s.size()>=2)
if(s.substr(s.size()-2)=="'s")
s=s.substr(0,s.size()-2);
}
if(stage==2)
{
if(s=="farmer"&&next=="john")
{
s="fj";
next="";
}
}
}
struct clause
{
vector<string> words;
vector<weightstruct> weights;
string s;
weightstruct weight;
void parse()
{
string word="";
bool iseq=false;
for(int j=0;j<s.length();j++)
{
if(s.at(j)=='$')
{
if(iseq)
{
word+="$";
words.pb(word);
word="";
iseq=false;
}
else
{
if(word!="") words.pb(word);
word="$";
iseq=true;
}
}
else
{
if(s.at(j)!=' ')
word+=(s.at(j));
//if(('a'<=s.at(j)&&s.at(j)<='z')||('A'<=s.at(j)&&s.at(j)<='Z')||('0'<=s.at(j)&&s.at(j)<='9')||(iseq&&s.at(j)!=' '))
// word+=('A'<=s.at(j)&&s.at(j)<='Z')?(s.at(j)-'A'+'a'):(s.at(j));
else
{
if(!iseq)
{
if(word!="")words.pb(word);
word="";
}
}
}
}
if(word!="") words.pb(word);
}
void calcweight()
{
weights.clear();
for(auto& w: words)weights.pb(getweight(w));
for(auto& w: weights) weight=weight+w;
}
void preprocess(int stage)
{
for(int i=0;i<words.size();i++)
{
string e="";
string* nx=&e;
if(i+1<words.size())
nx=&words[i+1];
preprocessword(stage,words[i],*nx,i);
}
}
string gettext(int minw)
{
bool iss=true;
string ans="";
for(int i=0;i<words.size();i++)
if(weights[i].word>=minw)
{
if(iss)
{
iss=false;
ans+=words[i];
}
else ans+=" "+words[i];
}
return ans;
}
};
struct sentence
{
vector<clause> clauses;
string s;
weightstruct weight;
void parse()
{
string cl="";
bool iseq=false;
for(int j=0;j<s.length();j++)
{
if(s.at(j)=='$') iseq=!iseq;
if(s.at(j)!=','|| iseq) cl+=s.at(j);
else
{
clause tmp;
tmp.s=cl;clauses.pb(tmp);cl="";
}
}
clause tmp;
tmp.s=cl;clauses.pb(tmp);
for(auto& c:clauses) c.parse();
int numclause=0;
for(int i=0;i<clauses.size();i++)
if(clauses[i].words.size()!=0)
{
auto tmp=clauses[i];
clauses[numclause]=tmp;
numclause++;
}
clauses.resize(numclause);
}
void calcweight()
{
weight=weightstruct();
for(auto& c : clauses)
{
c.calcweight();
weight=weight+c.weight;
}
}
void preprocess(int stage)
{
for(auto& c:clauses)
c.preprocess(stage);
}
string gettext(int minw)
{
bool iss=true;
string ans="";
for(auto& c:clauses)
if(c.weight.clause>=minw)
{
string tmp=c.gettext(minw);
if(tmp.length()>0)
{
if(iss)
{
iss=false;
ans=ans+tmp;
}
else ans+=","+tmp;
}
}
return ans;
}
};
struct file
{
vector<sentence> text;
void init(string tx)
{
string line;
stringstream stin(tx);
string sentencefile="";
bool iseq=false;
while(getline(stin,line))
{
if(line=="INPUT FORMAT")
{
sentence tmp;tmp.s=sentencefile;text.pb(tmp);
sentencefile=line;
while(getline(stin,line)&&line!="SAMPLE INPUT")
{
sentencefile+=' '+line;
}
sentence tmp2;tmp2.s=sentencefile;text.pb(tmp2);
sentencefile="";
}
if(line=="SAMPLE INPUT")
{
sentence tmp;tmp.s=sentencefile;text.pb(tmp);
sentencefile=line;
while(getline(stin,line))
{
sentencefile+=' '+line;
}
sentence tmp2;tmp2.s=sentencefile;text.pb(tmp2);
sentencefile="";
}
for(int i=0;i<line.length();i++)
{
if(line.at(i)=='$') iseq=!iseq;
if((line.at(i)!='.'&&line.at(i)!='?'&&line.at(i)!='!')|| iseq) sentencefile+=line.at(i);
else
{
sentence tmp;tmp.s=sentencefile;text.pb(tmp);
sentencefile="";
}
}
sentencefile+=' ';
}
sentence tmp;tmp.s=sentencefile;text.pb(tmp);
for(auto& i:text)i.parse();
}
void preprocess(int stage)
{
for(auto&x : text)
x.preprocess(stage);
}
void calcweight()
{
for(auto& x:text)
x.calcweight();
}
string gettextstring(int minw)
{
bool iss=true;
string ans="";
for(auto& c:text)
if(c.weight.sentence>=minw)
{
string tmp=c.gettext(minw);
if(tmp.length()>0)
{
if(iss)
{
iss=false;
ans=ans+tmp;
}
else ans+="."+tmp;
}
}
return ans;
}
};
string huffmancompress(string s)
{
vector<bool> enc = huffman::encode(s);
string ans;
for (int i = 0; i < ((int)enc.size() + 7) / 8; ++i) {
char out = 0;
for (int j = i; j < min(i + 8, (int)enc.size()); ++j) {
if (enc[j]) out += (1 << (j - i));
}
ans += out;
}
return ans;
}
int initsize=0;
int compressrate;
int targetsize;
int main() {
string input="";
string input_file = "soc1.txt";
//cin >> input_file;
string line;
cin>>compressrate;
ifstream stin(input_file);
while(getline(stin,line))
{
input+=line;
input+='\n';
}
initsize=input.length()-1;//remove last newline
file orig;
orig.init(input);
file final;
final=orig;
for(int i=0;i<5;i++)
{
final.preprocess(i);
}
targetsize=((100-compressrate)*initsize+99)/100;
final.calcweight();
int a=-10000;int b=10000;//(a,b]
while(a+1<b)
{
int c=(a+b)/2;
file tmp;
tmp.init(final.gettextstring(c));
tmp.calcweight();
tmp.preprocess(-1);
int sz=huffmancompress(tmp.gettextstring(-100000)).length();
if(sz>targetsize)
{
a=c;
}
else
{
b=c;
}
}
file output;
output.init(final.gettextstring(b));
output.calcweight();
output.preprocess(-1);
string s = output.gettextstring(-100000);
for (auto& c : s) if (c == '\r') c = ' ';
s = huffmancompress(s);
printf("%s\n", s.c_str());
/*int final_ratio = 10, orig_size = 0;
ifstream testin(input_file);
char c;
while (testin >> c) ++orig_size;
while (10 * ans.size() > final_ratio * orig_size) ans.pop_back();
cout << ans;*/
// WARNING: Huffman will CRASH if you pass a string with only one unique character
/*string orig = "test1234";
vector<bool> enc = huffman::encode(orig);
string dec = huffman::decode(enc);
if (orig != dec) {
cout << "Not a match!\n";
}
else {
cout << dec << '\n';
for (auto b : enc) cout << b;
cout << '\n';
cout << "Original length: " << orig.size() << '\n';
cout << "Compressed length: " << (enc.size() + 7) / 8 << '\n';
cout << "Percent compression: " << 100.0 - (double)100.0 * (enc.size() + 7) / 8 / orig.size() << "%\n";
}*/
}