Question

In: Computer Science

2. Using Lempel ziv compression code the following text KMMKABKAMKVVDAABCKMAA 10 marks

2. Using Lempel ziv compression code the following text
KMMKABKAMKVVDAABCKMAA
10 marks

Solutions

Expert Solution

The below c ++ code will give following output for your string guven in question

#include <bits/stdc++.h> 
using namespace std; 
vector<int> encoding(string s1) 
{ 
        cout << "Encoding\n"; 
        unordered_map<string, int> table; 
        for (int i = 0; i <= 255; i++) { 
                string ch = ""; 
                ch += char(i); 
                table[ch] = i; 
        } 

        string p = "", c = ""; 
        p += s1[0]; 
        int code = 256; 
        vector<int> output_code; 
        cout << "String\tOutput_Code\tAddition\n"; 
        for (int i = 0; i < s1.length(); i++) { 
                if (i != s1.length() - 1) 
                        c += s1[i + 1]; 
                if (table.find(p + c) != table.end()) { 
                        p = p + c; 
                } 
                else { 
                        cout << p << "\t" << table[p] << "\t\t"
                                << p + c << "\t" << code << endl; 
                        output_code.push_back(table[p]); 
                        table[p + c] = code; 
                        code++; 
                        p = c; 
                } 
                c = ""; 
        } 
        cout << p << "\t" << table[p] << endl; 
        output_code.push_back(table[p]); 
        return output_code; 
} 

void decoding(vector<int> op) 
{ 
        cout << "\nDecoding\n"; 
        unordered_map<int, string> table; 
        for (int i = 0; i <= 255; i++) { 
                string ch = ""; 
                ch += char(i); 
                table[i] = ch; 
        } 
        int old = op[0], n; 
        string s = table[old]; 
        string c = ""; 
        c += s[0]; 
        cout << s; 
        int count = 256; 
        for (int i = 0; i < op.size() - 1; i++) { 
                n = op[i + 1]; 
                if (table.find(n) == table.end()) { 
                        s = table[old]; 
                        s = s + c; 
                } 
                else { 
                        s = table[n]; 
                } 
                cout << s; 
                c = ""; 
                c += s[0]; 
                table[count] = table[old] + c; 
                count++; 
                old = n; 
        } 
} 
int main() 
{ 

        string s = "KMMKABKAMKVVDAABCKMAA"; 
        vector<int> output_code = encoding(s); 
        cout << "Output Codes are: "; 
        for (int i = 0; i < output_code.size(); i++) { 
                cout << output_code[i] << " "; 
        } 
        cout << endl; 
        decoding(output_code); 
} 

output

Encoding
String  Output_Code     Addition
K       75              KM      256
M       77              MM      257
M       77              MK      258
K       75              KA      259
A       65              AB      260
B       66              BK      261
KA      259             KAM     262
MK      258             MKV     263
V       86              VV      264
V       86              VD      265
D       68              DA      266
A       65              AA      267
AB      260             ABC     268
C       67              CK      269
KM      256             KMA     270
AA      267
Output Codes are: 75 77 77 75 65 66 259 258 86 86 68 65 260 67 256 267 

Decoding
KMMKABKAMKVVDAABCKMAA





Related Solutions

Write a code in order to make an android application of LZ77 compression algorithm for text...
Write a code in order to make an android application of LZ77 compression algorithm for text compression.
Question 5 (10 marks) Python Language What is the output of the following code ? (2...
Question 5 Python Language What is the output of the following code ? (2 points) a, b = 0, 1 while b < 10: print b a, b = b, a+b B. Explain List Comprehension (2 points) Given v = [1 3 5] w = [ [2*x, x**2] for x in v] What is the content of w? c. What is tuple ?   What is the difference between tuple and list ? (2 points) D. What is a module ?  ...
1. Using the following code identify the fault. 2. Using the following code indentify a test...
1. Using the following code identify the fault. 2. Using the following code indentify a test case that results in an error, but not a failure. public static int lastZero (int[] x) { //Effects: if x == null throw NullPointerException //else return the index of the last 0 in x. //Return -1 if 0 does not occur in x for (int i = 0; i < x.length; i++) { if (x[i] == 0) { return i; } } return -1;...
2. Operationally define the following: a) Sexual harassment (10 marks) b) Career success (10 marks)
2. Operationally define the following: a) Sexual harassment b) Career success
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Consider the following C code: (10 marks) // write a MIPS instruction to initialize s0 to...
Consider the following C code: // write a MIPS instruction to initialize s0 to 6 t0 = ((s03 - 93)2 + s0 ) << 2 t1 = t0 / 4 Q2.1: Write a MIPS program that performs the operation of the above C program. Q2.2: What is the value of $t0 and $t1 after running your MIPS program (write your answer in a comment at the end of the code). Submit your answer to Q2 in a file named A2_Q2.asm
Output and Debugging Questions (10 marks each) [20 Marks] Note: Provide a copy of the code...
Output and Debugging Questions (10 marks each) [20 Marks] Note: Provide a copy of the code and screen shot for the output in the solutions’ 1. Trace the following program and write the exact output for the following inputs. Explain each output. [10 Marks] a. Input of an array { 20, 80 , 63, 89 } b. Input of an array { 1, 2 ,3, 4} c. Input of an array { 100, 200 ,300, 400} d. Input of an...
write a fortran code on helical transcritical compression tube used in pulse tube cryocooler using R407...
write a fortran code on helical transcritical compression tube used in pulse tube cryocooler using R407 refrigerant. it is different than already replied question on chegg. please dont copy paste that answer i want genuine and correct code using fortran 77 only
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT