Skip to content

Commit 0a2d30e

Browse files
committed
Added 2 solutions to CF Educational 53 draft
1 parent 78e0108 commit 0a2d30e

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int n = Integer.parseInt(br.readLine());
9+
10+
String str = br.readLine();
11+
char[] arr = str.toCharArray();
12+
13+
boolean flag = false;
14+
15+
StringBuilder sb = new StringBuilder();
16+
for (int i=0; i<arr.length-1; i++) {
17+
if (arr[i] != arr[i+1]) {
18+
sb.append(arr[i]).append(arr[i+1]);
19+
System.out.println("YES");
20+
System.out.println(sb.toString());
21+
flag = true;
22+
break;
23+
}
24+
}
25+
26+
if (!flag) {
27+
System.out.println("NO");
28+
}
29+
}
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.Stack;
5+
6+
public class Main {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
int n = Integer.parseInt(br.readLine());
10+
int[] bookStatus = new int[n];
11+
12+
String[] books = br.readLine().split("\\s+");
13+
Stack<Integer> stack = new Stack<>();
14+
for (int i=books.length-1; i>=0; i--) {
15+
stack.push(Integer.parseInt(books[i]));
16+
}
17+
18+
int[] ans = new int[n];
19+
String[] backpack = br.readLine().split("\\s+");
20+
21+
for (int i=0; i<backpack.length; i++) {
22+
int book = Integer.parseInt(backpack[i]);
23+
if (bookStatus[book-1] != 0) {
24+
ans[i] = 0;
25+
continue;
26+
}
27+
28+
int count = 0;
29+
while (stack.peek() != book) {
30+
int popped = stack.pop();
31+
bookStatus[popped-1] = 1;
32+
count++;
33+
}
34+
35+
int popped = stack.pop();
36+
bookStatus[popped-1] = 1;
37+
count++;
38+
39+
ans[i] = count;
40+
}
41+
42+
for (int a : ans) {
43+
System.out.print(a + " ");
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)