forked from MiYazJE/Acepta-el-reto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp204.java
75 lines (55 loc) · 1.93 KB
/
p204.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author Rubén Saiz
*/
class Node {
char val;
Node left, right;
Node(char x) { val = x; }
public String toString() { return "" + val; }
}
public class p204 {
static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static int index;
static void readTree(Node node, String input) {
if (index == input.length()) return;
char temp = input.charAt(index++);
node.left = new Node(temp);
if (temp != '.' && temp != '*') {
readTree(node.left, input);
}
if (index == input.length()) return;
temp = input.charAt(index++);
node.right = new Node(temp);
if (temp != '.' && temp != '*') {
readTree(node.right, input);
}
}
static boolean correcto;
static int esArbolDeNAvidad(Node node, int bolasIzq, int bolasDer, char direction) {
if (node == null || node.val == '.')
return (direction == 'l') ? bolasIzq : bolasDer;
int sum;
sum = (node.val == 'Y' && node.left.val == '*') ? 1 : 0;
int left = esArbolDeNAvidad(node.left, bolasIzq + sum, bolasDer, 'l');
sum = (node.val == 'Y' && node.right.val == '*') ? 1 : 0;
int right = esArbolDeNAvidad(node.right, bolasIzq, bolasDer + sum, 'r');
if (Math.abs(left - right) > 1) correcto = false;
return left + right;
}
public static void main(String[] args) throws IOException {
String input;
while (true) {
input = reader.readLine();
if (input == null) return;
Node tree = new Node(input.charAt(0));
index = 1;
readTree(tree, input);
correcto = true;
esArbolDeNAvidad(tree, 0, 0, 'l');
System.out.println( (correcto) ? "OK" : "KO" );
}
}
}