-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
191 lines (151 loc) · 5.7 KB
/
Main.java
File metadata and controls
191 lines (151 loc) · 5.7 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
TTree tTree = new TTree();
Scanner scanner = new Scanner(System.in);
int numberOfCommands = scanner.nextInt();
int command,data,numberOfAnswers = 0;
int[] answers = new int[numberOfCommands];
for(int i = 0; i < numberOfCommands; i ++){
command = scanner.nextInt();
data = scanner.nextInt();
if(command == 1){
tTree.insertValue(Integer.toBinaryString(data));
}else if (command == 2){
int xor = tTree.maxExclusiveOr(data);
answers[numberOfAnswers] = xor;
numberOfAnswers++;
}
}
for(int i = 0; i < numberOfAnswers; i++){
System.out.println(answers[i]);
}
}
}
class TTreeNode{
private boolean finished = false;
private TTreeNode[] nodeChildren = new TTreeNode[2]; //For this node's children
private String str = "";
public boolean isFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
public TTreeNode[] getNodeChildren() {
return nodeChildren;
}
public void setNodeChildren(TTreeNode[] nodeChildren) {
this.nodeChildren = nodeChildren;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
class TTree {
int maxKeysLength = 0; //Represents the maximum length of keys
static TTreeNode rNode = new TTreeNode();
/*int maxExclusiveOr(int number) {
TTreeNode tempNode = null;
String answer = "";
String binary = Integer.toBinaryString(number);
int difference = maxKeysLength - binary.length();
if (difference == 0) { // both have same size
answer = binary;
} else if (difference > 0) { //Setting 0's for the rest of digits cause we have free space
for (int i = 0; i < difference; i++) {
answer += "0";
}
answer += binary; // copying the rest of the string
} else { // cutting the string
answer = binary.substring(Math.abs(difference)); // cutting LSB's from the string
}
int firstOccurrence = answer.indexOf("0"); // looks for the first zero in the string
if (firstOccurrence == -1) {
//The string doesn't have a 0 so we can only use 0 for xor
return number;
} else {
String searchFor = answer.substring(firstOccurrence--);
tempNode = find(searchFor, rNode);
if (tempNode != null)
return Integer.parseInt(tempNode.getStr(), 2) ^ number;
if(tempNode == null)
return number;
}
return number;
}
}*/
void insertValue(String binary) { //Inserts a new key in the tree
int binaryLength = binary.length();
if (binaryLength > maxKeysLength) {
maxKeysLength = binaryLength;
}
int index = 0;
TTreeNode tempNode = rNode;
for (int i = 0; i < binaryLength; i++) {
index =Character.getNumericValue(binary.charAt(i));
if (tempNode.getNodeChildren()[index] == null) {
tempNode.getNodeChildren()[index] = new TTreeNode();
tempNode.getNodeChildren()[index].setStr(tempNode.getStr() + index);
}
tempNode = tempNode.getNodeChildren()[index];
}
tempNode.setFinished(true);
}
TTreeNode find(String string, TTreeNode tempNode) { // Thanks to another indian youtuber
if (string.length() == 0 && tempNode.isFinished() == true) { ////the current node is the end of a key and our string is empty
return tempNode;
} else if (string.length() == 0) { //the current node ain't end of a key
return null;
}
int index = Character.getNumericValue(string.charAt(0));
TTreeNode a = null;
TTreeNode b = null;
int direction = index ^ 1; //shows the direction
if (tempNode.getNodeChildren()[direction] != null) {
a = find(string.substring(1), tempNode.getNodeChildren()[direction]);
}
if (a != null)
return a;
if (tempNode.getNodeChildren()[(direction + 1) % 2] != null) {
b = find(string.substring(1), tempNode.getNodeChildren()[(direction + 1) % 2]);
}
return b;
}
int maxExclusiveOr(int n) {
TTreeNode tempNode = null;
String binary = Integer.toBinaryString(n);
String answer = "";
int difference = maxKeysLength - binary.length();
if (difference > 0) {
for (int i = 0; i < maxKeysLength - binary.length(); i++) {
answer += "0";
}
answer = answer.concat(binary);
} else if (binary.length() > maxKeysLength) {
answer = binary.substring(binary.length() - maxKeysLength);
} else {
answer = binary;
}
int i;
i = answer.indexOf("0");
if (i == -1) {
return n;
}
if (i != -1) {
String searchFor = "";
searchFor = searchFor.concat(answer.substring(i--));
tempNode = this.find(searchFor, rNode);
}
if (tempNode == null)
return n;
else if (tempNode != null) {
return Integer.parseInt(tempNode.getStr(), 2) ^ n;
}
return n;
}
}