Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <cstdlib>
- #include <exception>
- #include <fstream>
- #include <ios>
- #include <iostream>
- #include <istream>
- #include <limits>
- #include <map>
- #include <ostream>
- #include <stdexcept>
- #include <string>
- #include <vector>
- using namespace std;
- void initdict(map<string,int> &enc,map<int,string> &dec, int &last)
- {
- last= 0;
- enc.clear();
- dec.clear();
- for (long long c = 0; c <= 255; c++,last++)
- {
- string s = {char(c)};
- enc[s] = last;
- dec[last] = s;
- }
- }
- void encodefile(string input, string output, map<string,int> enc, int last)
- {
- ifstream cin(input);
- ofstream cout (output);
- char c;
- string s;
- while (cin.get(c))
- {
- s.push_back(c);
- if (enc.count(s) == 0)
- {
- enc[s] = last;
- last++;
- s.pop_back();
- cout << enc[s]<< " ";
- s = {c};
- }
- }
- if (!s.empty())
- cout << enc[s];
- }
- void decodefile(string input, string output, map<int,string> dec, int last)
- {
- ifstream cin(input);
- ofstream cout(output);
- int code,lcode;
- cin >> lcode;
- cout << dec[lcode];
- while (cin>> code)
- {
- if (dec.count(code))
- {
- cout << dec[code];
- dec[last]=dec[lcode]+dec[code][0];
- }
- else
- {
- cout << dec[lcode]+dec[lcode][0];
- dec[last]=dec[lcode]+dec[lcode][0];
- }
- last++;
- lcode=code;
- }
- }
- int main()
- {
- map<string,int>enc;
- map<int,string> dec;
- int last;
- initdict(enc, dec,last);
- string file1 = "input.txt";
- string file2 = "output.txt";
- string file3 = "input1.txt";
- encodefile(file1,file2,enc,last);
- decodefile(file2, file3, dec,last);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement