-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeSumming.cpp
48 lines (48 loc) · 931 Bytes
/
TreeSumming.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
#include <stdio.h>
#define empty 0
int ans;
int solve(int sum, int target, char *c) {
int flag = 0, leaf = 0, token, neg;
while(*c == ' ' || *c == '\n')
*c = getchar();
if(*c == '(') {
token = 0, flag = 0, neg = 1;
while(*c = getchar()) {
if(*c >= '0' && *c <= '9')
token = token*10 + *c-'0', flag = 1;
else {
if(*c == '-') neg = -1;
else break;
}
}
token *= neg;
while(*c == ' ' || *c == '\n')
*c = getchar();
if(flag == 0) {
return empty;
}
int left = solve(sum+token, target, c);
while((*c = getchar()) != '(');
int right = solve(sum+token, target, c);
while((*c = getchar()) != ')');
if(left == 0 && right == 0) {
if(sum+token == target)
ans = 1;
}
return 1;
}
}
int main() {
int target;
char c;
while(scanf("%d", &target) == 1) {
ans = 0;
c = getchar();
solve(0, target, &c);
if(ans == 1)
puts("yes");
else
puts("no");
}
return 0;
}