-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc1609_decompress.cpp
106 lines (95 loc) · 3.15 KB
/
aoc1609_decompress.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ************************************************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ****************************************************************** nuo *** */
#include "iostream"
#include "sstream"
#include "vector"
#include "string"
long decompress_omega(std::string s);
int decompress_alpha(std::string s);
int main()
{
std::string s;
long len1;
long len2;
std::cin >> s;
len1 = decompress_alpha(s);
len2 = decompress_omega(s);
std::cout << "Star 1 : " << len1 << std::endl;
std::cout << "Star 2 : " << len2 << std::endl;
return (0);
}
long decompress_omega(std::string s)
{
std::size_t L;
std::size_t R;
std::string sub;
long len;
char c;
int a;
int b;
len = 0;
L = 0;
R = 0;
if (s.find('(') == std::string::npos)
return ((long) s.length());
while (s.find('(') != std::string::npos)
{
L = s.find('(');
R = s.find(')');
if (R != std::string::npos)
{
std::stringstream ss(s.substr(L + 1, R - 1));
ss >> a >> c >> b;
// debug
//std::cout << a << ' ' << c << ' ' << b << std::endl;
len += L;
s = s.substr(R + 1);
len += b * decompress_omega(s.substr(0, a));
s = s.substr(a);
}
}
return (len + (long) s.length());
}
int decompress_alpha(std::string s)
{
std::size_t L;
std::size_t R;
char c;
int len;
int a;
int b;
int i;
len = i = 0;
while (s[i])
{
s = s.substr(i);
L = s.find('(');
if (L != std::string::npos)
{
R = s.find(')');
if (R != std::string::npos)
{
std::stringstream ss(s.substr(L + 1, R - 1));
ss >> a >> c >> b;
len += L + a * b;
i = R + a + 1;
}
}
else
{
len += (int) s.length();
break;
}
}
return (len);
}