-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathURI1063RailsAgainTracingMovements.java
More file actions
49 lines (43 loc) · 1.43 KB
/
URI1063RailsAgainTracingMovements.java
File metadata and controls
49 lines (43 loc) · 1.43 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Stack;
/**
* See
* <a href="https://www.urionlinejudge.com.br/judge/en/problems/view/1063">Rails
* Again... Tracing Movements</a>
*
* @author Brian Yeicol Restrepo Tangarife
*/
public class URI1063RailsAgainTracingMovements {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
String l;
String[] sequence, desired;
int n, current;
while (!(l = in.readLine()).equals("0")) {
n = Integer.parseInt(l);
sequence = read().split("\\s");
desired = read().split("\\s");
Stack<String> stack = new Stack<>();
current = 0;
for (int i = 0; i < n; i++) {
stack.push(sequence[i]);
out.print("I");
while (stack.size() > 0 && desired[current].equals(stack.lastElement())) {
current++;
stack.pop();
out.print("R");
}
}
out.println(stack.size() > 0 ? " Impossible" : "");
}
out.close();
}
private static String read() throws IOException {
return in.readLine();
}
}