This repository was archived by the owner on Dec 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_solution.cpp
More file actions
58 lines (49 loc) · 1.65 KB
/
regex_solution.cpp
File metadata and controls
58 lines (49 loc) · 1.65 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
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
int t;
cin >> t;
cin.ignore();
int answer = 0;
bool do_add = true, do_mul = true;
regex pattern(R"((do_mul\(\)|do_add\(\)|don't_mul\(\)|don't_add\(\)|add\(\d+,\d+\)|mul\(\d+,\d+\)))");
while(t--) {
string s;
getline(cin, s);
auto words_begin = sregex_iterator(s.begin(), s.end(), pattern);
auto words_end = sregex_iterator();
for(sregex_iterator i = words_begin; i != words_end; ++i) {
string item = i->str();
if(item == "do_mul()") {
do_mul = true;
}
else if(item == "do_add()") {
do_add = true;
}
else if(item == "don't_mul()") {
do_mul = false;
}
else if(item == "don't_add()") {
do_add = false;
}
else if(item.substr(0, 3) == "add" && do_add) {
string nums = item.substr(4, item.length() - 5); // remove add( and )
size_t comma = nums.find(',');
int a = stoi(nums.substr(0, comma));
int b = stoi(nums.substr(comma + 1));
answer += a + b;
}
else if(item.substr(0, 3) == "mul" && do_mul) {
string nums = item.substr(4, item.length() - 5); // remove mul( and )
size_t comma = nums.find(',');
int a = stoi(nums.substr(0, comma));
int b = stoi(nums.substr(comma + 1));
answer += a * b;
}
}
}
cout << answer << endl;
return 0;
}