Guest User

Untitled

a guest
Dec 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> encoding(string s1){
  4. cout<<"Encoding\n";
  5. unordered_map<string, int> table;
  6. for(int i=0; i<=255; i++){
  7. string ch= "";
  8. ch+= char(i);
  9. table[ch]= i;
  10. }
  11.  
  12. string p= "", c="";
  13. p+= s1[0];
  14. int code= 256;
  15. vector<int> output_code;
  16. cout<<"String\tOutput_Code\tAddition\n";
  17. for(int i=0; i<s1.length(); i++){
  18. if (i!= s1.length()-1)
  19. c+= s1[i+1];
  20. if (table.find(p+c)!= table.end()){
  21. p= p+c;
  22. }else{
  23. cout<<p<<"\t"<<table[p]<<"\t\t"<<p+c<<"\t"<<code<<endl;
  24. output_code.push_back(table[p]);
  25. table[p+c]= code;
  26. code++;
  27. p= c;
  28. }
  29. c="";
  30. }
  31. cout<<p<<"\t"<<table[p]<<endl;
  32. output_code.push_back(table[p]);
  33. return output_code;
  34. }
  35.  
  36. void decoding(vector<int> op){
  37. cout<<"\nDecoding\n";
  38. unordered_map<int, string> table;
  39. for(int i=0; i<=255; i++){
  40. string ch= "";
  41. ch+= char(i);
  42. table[i]= ch;
  43. }
  44. int old= op[0], n;
  45. string s= table[old];
  46. string c= "";
  47. c+= s[0];
  48. cout<<s;
  49. int code= 256;
  50. for(int i=0; i<op.size()-1; i++){
  51. n= op[i+1];
  52. if(table.find(n)== table.end() ){
  53. s= table[old];
  54. s= s+c;
  55. }else{
  56. s= table[n];
  57. }
  58. cout<<s;
  59. c= "";
  60. c+= s[0];
  61. table[code]= table[old]+c;
  62. code++;
  63. old= n;
  64. }
  65.  
  66. }
  67. int main(){
  68.  
  69. string s;
  70. cout<<"Enter text: ";
  71. cin>>s;
  72. vector<int> output_code= encoding(s);
  73. cout<<"Output Codes are: ";
  74. for(int i=0; i<output_code.size(); i++){
  75. cout<<output_code[i]<<" ";
  76. }
  77. cout<<endl;
  78. decoding(output_code);
  79. }
Add Comment
Please, Sign In to add comment