-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIfElse.cpp
More file actions
51 lines (45 loc) · 933 Bytes
/
IfElse.cpp
File metadata and controls
51 lines (45 loc) · 933 Bytes
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
#include <fstream>
#include <cstring>
using namespace std;
ifstream cin("ifelse.in");
ofstream cout("ifelse.out");
const int NMAX = 500005;
const char sep[] = " ";
char a[NMAX];
int n;
struct Cuv {
char sir[10];
};
int Rezolvare(char *a) {
int top = 0;
int op = 0;
Cuv st[NMAX];
char *p = strtok(a, sep);
while (p != NULL) {
if (strcmp(p, "if") == 0) {
strcpy(st[++top].sir, p);
}
else if (strcmp(p, "else") == 0) {
if (top == 0) {
op++;
strcpy(st[++top].sir, "if");
}
else {
top--;
}
}
p = strtok(NULL, sep);
}
if (top % 2 == 1)
return -1;
return op + (top / 2);
}
int main() {
cin >> n;
cin.ignore();
for (int i = 1; i <= n; i++) {
cin.getline(a, NMAX);
cout << Rezolvare(a) << '\n';
}
return 0;
}