0% found this document useful (0 votes)
17 views

Code Assignment

The document contains code for implementing different compression algorithms: 1) Run Length Encoding which replaces consecutive repeating characters with a count and character 2) LZW Encoding which maps strings to integers based on a dictionary 3) Huffman Coding which assigns variable length codes to characters based on frequency 4) Diatomic Encoding which maps common digrams to single characters The codes take a string as input, apply the encoding algorithm, and output the encoded string.

Uploaded by

Nahim's kitchen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Code Assignment

The document contains code for implementing different compression algorithms: 1) Run Length Encoding which replaces consecutive repeating characters with a count and character 2) LZW Encoding which maps strings to integers based on a dictionary 3) Huffman Coding which assigns variable length codes to characters based on frequency 4) Diatomic Encoding which maps common digrams to single characters The codes take a string as input, apply the encoding algorithm, and output the encoded string.

Uploaded by

Nahim's kitchen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Run Lenth Code

#include<bits/stdc++.h>
using namespace std;

int main()
{
freopen("input.txt","r",stdin) ;
freopen("output.txt","w",stdout);
string code;
cout<<"Enter the Code: ";
cin>>code;
cout<<"Run Length Encoding is: ";
for(int i=0;i<code.size();i++)
{
char ch=code[i];
int cnt=0;
while(i<code.size() && code[i]==ch)
{
cnt++, i++;
}
i--;
cout<<cnt<<ch;
}
cout<<endl;
}
Input: AAABBBBCDDDDDDEEE
Output: Enter the Code: Run Length Encoding is: 3A4B1C6D3E
LZW Code
#include<bits/stdc++.h>
using namespace std;

#define fileinput freopen("input.txt","r",stdin) ;


#define fileoutput freopen("output.txt","w",stdout);

int main()
{
fileinput
fileoutput
string s;
cin>>s;

set<char>ch;
for(char x:s)ch.insert(x);

map<string,int>mp;
int cnt=1;
for(char x:ch)
{
string ss;
ss+=x;
mp[ss]=cnt;
cnt++;
}
//for(auto it:mp)cout<<it.first<<" "<<it.second<<endl;

string ss="";
ss+=s[0];
vector<int>ans;
for(int i=1;i<s.size();i++)
{
int f=mp[ss];
ss+=s[i];
if(mp[ss]==0)
{
mp[ss]=cnt;
cnt++;
ans.push_back(f);
ss="";
ss+=s[i];
}
}
//cout<<ss<<endl;
//cout<<mp[ss]<<endl;
ans.push_back(mp[ss]);
for(int i=0;i<ans.size();i++)cout<<ans[i]-1;
cout<<endl;
}

Input: BAABABBBAABBBBAA
Output: 1002163670
Huffman Code
#include<bits/stdc++.h>
using namespace std;
#define fileinput freopen("input.txt","r",stdin) ;
#define fileoutput freopen("output.txt","w",stdout);

struct Node
{
char ch;
int freq;
Node *left, *right;
};
Node* getNode(char ch, int freq, Node* left, Node* right)
{
Node* node = new Node();

node->ch = ch;
node->freq = freq;
node->left = left;
node->right = right;
return node;
}
struct comp
{
bool operator()(Node* l, Node* r)
{
return l->freq > r->freq;
}
};
void encode(Node* root, string str,map<char, string> &huffmanCode)
{
if (root == nullptr)
return;

// found a leaf node


if (!root->left && !root->right) {
huffmanCode[root->ch] = str;
}
encode(root->left, str + "0", huffmanCode);
encode(root->right, str + "1", huffmanCode);
}
void buildHuffmanTree(string text)
{

unordered_map<char, int> freq;


for (char ch: text) {
freq[ch]++;
}
priority_queue<Node*, vector<Node*>, comp> pq;
for (auto pair: freq) {
pq.push(getNode(pair.first, pair.second, nullptr, nullptr));
}
while (pq.size() != 1)
{
Node *left = pq.top(); pq.pop();
Node *right = pq.top(); pq.pop();
int sum = left->freq + right->freq;
pq.push(getNode('\0', sum, left, right));
}
Node* root = pq.top();
map<char, string> huffmanCode;
encode(root, "", huffmanCode);
cout << "Huffman Codes are :\n" << '\n';
for(int i=0;i<text.size();i++)cout<<huffmanCode[text[i]];
cout<<endl;
}

int main()
{
fileinput
fileoutput
//string text = "Huffman coding is a data compression algorithm.";
string text;
getline(cin,text);

buildHuffmanTree(text);
return 0;
}

Input: ABCDE
Output: Huffman Codes are : 010011110110
Diatomic Code
#include<bits/stdc++.h>
using namespace std;

#define fileinput freopen("input.txt","r",stdin) ;


#define fileoutput freopen("output.txt","w",stdout);

int main()
{
fileinput
fileoutput

string s;
getline(cin,s);

map<string,int>mp;
stringstream ss(s);
string word;
while(ss>>word)
{
for(int i=0; i<word.size()-1; i++)
{
string x;
x+=word[i];
x+=word[i+1];
mp[x]++;
}
}
vector<pair<int,string> > v;
for(auto it:mp)
{
v.push_back({it.second,it.first});
}
sort(v.begin(),v.end());
char ch='0';
map<string,char>spchar;
for(int i=v.size()-1; i>=0; i--)
{
spchar[v[i].second]= 'a' + ch;
ch++;
}
string ans = s;
for(int i=v.size()-1; i>=0; i--)
{
string par=v[i].second;
map<int,int>mp1;
for(int j=0; j<ans.size()-1; j++)
{
string x;
x+=ans[j];
x+=ans[j+1];
//cout<<x<<" ";
if(x==par)
{
mp1[j]=1;
j++;
}
}
string ans1="";
for(int j=0; j<ans.size(); j++)
{
if(mp1[j]!=1)ans1+=ans[j];
else
{
string x;
x+=ans[j];
x+=ans[j+1];
ans1+=spchar[x];
j++;
}
}
ans=ans1;
}
cout<<ans<<endl;
}

Input: I am X. I am a studying in HSTU.


Output: I ’ ™ I ’ a –”y‘g ‘ Hœš

You might also like