-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunLengthEncodingAndDecoding.cpp
More file actions
69 lines (61 loc) · 1.58 KB
/
RunLengthEncodingAndDecoding.cpp
File metadata and controls
69 lines (61 loc) · 1.58 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
string ExpendString(const string& expd)
{
int count = 1, temp; vector<char> countVec;
for (int i = 0; i < expd.size(); i = i + 2)
{
for ( int j = 0; j < expd[i] -'0'; j++)
{
countVec.push_back(expd[i+1]);
}
}
string mar1 = new char[countVec.size()+1];
for (int x = 0; x < countVec.size(); x++)
{
mar1[x] = countVec[x];
cout << "mar1[" << x << "] " << " " << mar1[x] << endl;
}
return mar1;
}
string CompressString(const string& mar)
{
int count = 1, temp; vector<char> countVec;
string mar1 = new char[mar.size()+1];
//char mar1[10];
for (int i = 1; i < mar.size(); ++i)
{
if(mar[i-1] == mar[i])
{
++count; temp = count;
//cout << mar[i] <<" " << count << endl;
}
else
{
temp = count; countVec.push_back(temp + '0'); countVec.push_back(mar[i-1]); count = 1; //cout << mar[i] <<" " << count << endl;
//cout << mar[i-1] <<" temp " << temp << endl;
}
}
countVec.push_back(temp + '0');
countVec.push_back(mar[mar.size()-1]);
cout << "countVec.size() " << countVec.size() << endl;
for (int x = 0; x < countVec.size(); x++)
{
mar1[x] = countVec[x];
cout << "mar1[" << x << "] " << " " << mar1[x] << endl;
}
cout << ExpendString(mar1);
return mar1;
}
int main()
{
string mar = "AAAABBBCCDA"; string mar2 = "4A3B2C1D2A";
string mar1 = CompressString(mar);
//cout << CompressString(mar) << endl;
cout << mar1 << endl;
cout << ExpendString(mar2);
return 0;
}