Home AtCoder. ABC 237 D LR insertion
Post
Cancel

AtCoder. ABC 237 D LR insertion

[Link] https://AtCoder.jp/contests/abc237/tasks/abc237_d

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
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringBuilder sb = new StringBuilder();
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = toi(br.readLine());
		String s = br.readLine();
		Node[] nodes = new Node[n + 1];
		for(int i = 0; i <= n; i++) nodes[i] = new Node(i);

		for(int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			if(ch == 'L') {
				nodes[i].left = nodes[i + 1];
			} else nodes[i].right = nodes[i + 1];
		}

		solve(nodes[0]);
		println(sb);
	}

	static void solve(Node node) {
		if(node.left != null) solve(node.left);
		sb.append(node.idx).append(" ");
		if(node.right != null) solve(node.right);
	}

	static class Node{
		Node left;
		Node right;
		int idx;
		Node(int idx) { this. idx = idx; }
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.