Skip to content

Commit 14aeab6

Browse files
committed
Add solutions
1 parent 21c6e1b commit 14aeab6

File tree

7 files changed

+688
-0
lines changed

7 files changed

+688
-0
lines changed

create-tree/src/by/bsu/fpmi/Main.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package by.bsu.fpmi;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
10+
public class Main {
11+
public static void main(String[] args) throws IOException {
12+
BinaryTree binaryTree = new BinaryTree();
13+
14+
List<String> strings = Files.readAllLines(Paths.get("input.txt"));
15+
strings.forEach(x -> binaryTree.add(Long.parseLong(x.trim())));
16+
17+
binaryTree.traversePreOrder("output.txt");
18+
}
19+
}
20+
21+
class BinaryTree {
22+
private TreeNode root;
23+
24+
public void add(long value) {
25+
root = addRecursive(root, value);
26+
}
27+
28+
private TreeNode addRecursive(TreeNode current, long value) {
29+
if (current == null) {
30+
return new TreeNode(value);
31+
}
32+
33+
long currentValue = current.value;
34+
35+
if (value < currentValue) {
36+
current.leftNode = addRecursive(current.leftNode, value);
37+
} else {
38+
if (value > currentValue) {
39+
current.rightNode = addRecursive(current.rightNode, value);
40+
}
41+
}
42+
43+
return current;
44+
}
45+
46+
public void traversePreOrder(String filePath) throws IOException {
47+
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
48+
traversePreOrder(writer, root);
49+
writer.close();
50+
}
51+
52+
private void traversePreOrder(BufferedWriter writer, TreeNode node) throws IOException {
53+
if (node != null) {
54+
writer.write(node.value + "\n");
55+
traversePreOrder(writer, node.leftNode);
56+
traversePreOrder(writer, node.rightNode);
57+
}
58+
}
59+
60+
private static class TreeNode {
61+
private long value;
62+
private TreeNode leftNode;
63+
private TreeNode rightNode;
64+
65+
TreeNode(long value) {
66+
this.value = value;
67+
}
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package by.bsu.fpmi;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.List;
10+
11+
public class Main {
12+
public static void main(String[] args) throws IOException {
13+
Path path = Paths.get("input.txt");
14+
List<String> lines = Files.readAllLines(path);
15+
16+
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
17+
writer.write("" + SubstringFinder.indexOf(lines.get(1) + lines.get(1), lines.get(2)));
18+
writer.close();
19+
}
20+
}
21+
22+
class SubstringFinder {
23+
public static int indexOf(String source, String pattern) {
24+
int sourceLength = source.length();
25+
int patternLength = pattern.length();
26+
27+
if (patternLength == 0) {
28+
return 0;
29+
}
30+
31+
int[] prefix = createPrefixFunction(pattern);
32+
int sourceIndex = 0;
33+
int patternIndex = 0;
34+
35+
while (sourceIndex < sourceLength) {
36+
if (source.charAt(sourceIndex) == pattern.charAt(patternIndex)) {
37+
++sourceIndex;
38+
if (++patternIndex == patternLength) {
39+
return sourceIndex - patternLength;
40+
}
41+
} else {
42+
if (patternIndex > 0) {
43+
patternIndex = prefix[patternIndex - 1];
44+
} else {
45+
++sourceIndex;
46+
}
47+
}
48+
}
49+
50+
return -1;
51+
}
52+
53+
private static int[] createPrefixFunction(String line) {
54+
int[] prefix = new int[line.length()];
55+
int leftIndex = 0;
56+
57+
for (int i = 1; i < line.length(); i++) {
58+
if (line.charAt(i) == line.charAt(leftIndex)) {
59+
prefix[i] = ++leftIndex;
60+
} else {
61+
if (leftIndex != 0) {
62+
leftIndex = prefix[leftIndex - 1];
63+
}
64+
}
65+
}
66+
67+
return prefix;
68+
}
69+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package by.bsu.fpmi;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
10+
public class Main extends Thread {
11+
public static void main(String[] args) {
12+
new Thread(null, new Main(), "Deleting value from tree", 64 * 1024 * 1024).start();
13+
}
14+
15+
@Override
16+
public void run() {
17+
try {
18+
BinaryTree binaryTree = new BinaryTree();
19+
20+
List<String> strings = Files.readAllLines(Paths.get("input.txt"));
21+
long toDeleteValue = Long.parseLong(strings.get(0));
22+
23+
strings.remove(0);
24+
strings.remove(0);
25+
26+
strings.forEach(x -> binaryTree.add(Long.parseLong(x.trim())));
27+
binaryTree.remove(toDeleteValue);
28+
29+
binaryTree.traversePreOrder("output.txt");
30+
} catch (IOException e) {
31+
System.err.println(e.getMessage());
32+
}
33+
}
34+
}
35+
36+
class BinaryTree {
37+
private TreeNode root;
38+
39+
public void add(long value) {
40+
root = addRecursive(root, value);
41+
}
42+
43+
public void remove(long value) {
44+
root = removeRecursive(root, value);
45+
}
46+
47+
public void traversePreOrder(String filePath) throws IOException {
48+
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
49+
traversePreOrder(writer, root);
50+
writer.close();
51+
}
52+
53+
private TreeNode addRecursive(TreeNode current, long value) {
54+
if (current == null) {
55+
return new TreeNode(value);
56+
}
57+
58+
long currentValue = current.value;
59+
60+
if (value == currentValue) {
61+
return current;
62+
}
63+
64+
if (value < currentValue) {
65+
current.leftNode = addRecursive(current.leftNode, value);
66+
} else {
67+
current.rightNode = addRecursive(current.rightNode, value);
68+
}
69+
70+
return current;
71+
}
72+
73+
private TreeNode removeRecursive(TreeNode current, long value) {
74+
if (current == null) {
75+
return null;
76+
}
77+
78+
long currentValue = current.value;
79+
80+
if (value == currentValue) {
81+
if (current.leftNode == null && current.rightNode == null) {
82+
return null;
83+
}
84+
if (current.rightNode == null) {
85+
return current.leftNode;
86+
}
87+
if (current.leftNode == null) {
88+
return current.rightNode;
89+
}
90+
91+
long smallestValue = findSmallestValue(current.rightNode);
92+
current.value = smallestValue;
93+
current.rightNode = removeRecursive(current.rightNode, smallestValue);
94+
return current;
95+
}
96+
97+
if (value < currentValue) {
98+
current.leftNode = removeRecursive(current.leftNode, value);
99+
} else {
100+
current.rightNode = removeRecursive(current.rightNode, value);
101+
}
102+
103+
return current;
104+
}
105+
106+
private long findSmallestValue(TreeNode root) {
107+
return (root.leftNode == null) ? root.value : findSmallestValue(root.leftNode);
108+
}
109+
110+
private void traversePreOrder(BufferedWriter writer, TreeNode node) throws IOException {
111+
if (node != null) {
112+
writer.write(node.value + "\n");
113+
traversePreOrder(writer, node.leftNode);
114+
traversePreOrder(writer, node.rightNode);
115+
}
116+
}
117+
118+
private static class TreeNode {
119+
private long value;
120+
private TreeNode leftNode;
121+
private TreeNode rightNode;
122+
123+
TreeNode(long value) {
124+
this.value = value;
125+
}
126+
}
127+
}

frogs/src/by/bsu/fpmi/Main.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package by.bsu.fpmi;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
public class Main {
12+
public static void main(String[] args) throws IOException {
13+
List<String> strings = Files.readAllLines(Paths.get("input.txt"));
14+
Integer[] inputArray = Arrays.stream(strings.get(1).split("\\s+"))
15+
.mapToInt(Integer::parseInt)
16+
.boxed()
17+
.toArray(Integer[]::new);
18+
19+
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
20+
writer.write(forwardSolution(inputArray) + "");
21+
writer.close();
22+
}
23+
24+
public static int backSolution(Integer[] array) {
25+
if (array.length == 1) {
26+
return array[0];
27+
}
28+
int[] solution = new int[array.length + 1];
29+
solution[1] = array[0];
30+
solution[2] = -1;
31+
32+
for (int i = 3; i < solution.length; ++i) {
33+
solution[i] = Math.max(solution[i - 2], solution[i - 3]) + array[i - 1];
34+
}
35+
return solution[array.length];
36+
}
37+
38+
public static int forwardSolution(Integer[] array) {
39+
if (array.length == 1) {
40+
return array[0];
41+
}
42+
43+
int[] solution = new int[array.length];
44+
solution[0] = array[0];
45+
solution[1] = -1;
46+
for (int i = 0; i <= solution.length - 3; ++i) {
47+
solution[i + 2] = Math.max(solution[i+2], solution[i] + array[i + 2]);
48+
if (i < solution.length - 3) {
49+
solution[i + 3] = Math.max(solution[i+3], solution[i] + array[i + 3]);
50+
}
51+
}
52+
53+
return solution[solution.length - 1];
54+
}
55+
}

0 commit comments

Comments
 (0)