-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
54 lines (50 loc) · 1.39 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <sstream>
std::string encode(const std::string& input)
{
std::ostringstream result;
std::string::const_iterator temp = input.begin();
int count = 1;
for(std::string::const_iterator it = std::next(input.begin()); it != input.end(); ++it)
{
if(*temp != *it)
{
result << count << *temp;
temp = it;
count = 1;
} else {
count++;
}
}
result << count << *temp;
return result.str();
}
std::string decode(const std::string& input)
{
std::ostringstream temp, result;
for(std::string::const_iterator it = input.begin(); it != input.end(); ++it)
{
if(!isdigit(*it))
{
result << std::string(std::stoi(temp.str()), (*it));
temp.str("");
} else {
temp << (*it);
}
}
return result.str();
}
int main()
{
std::string line = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
std::string encoded = encode(line);
std::string decoded = decode(encoded);
std::cout << "line:" << std::endl;
std::cout << line << std::endl;
std::cout << "encoded:" << std::endl;
std::cout << encoded << std::endl;
std::cout << "decoded:" << std::endl;
std::cout << decoded << std::endl;
return 0;
}