-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay6.java
More file actions
35 lines (32 loc) · 801 Bytes
/
Day6.java
File metadata and controls
35 lines (32 loc) · 801 Bytes
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
package Algorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Day6 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
for(int i=0; i<N; i++) {
String line= br.readLine();
Stack<Character> s = new Stack<>();
for(int j=0; j<line.length(); j++) {
char c = line.charAt(j);
if(c == ' ') {
while(!s.isEmpty()) {
sb.append(s.pop());
}
sb.append(c);
}else {
s.push(c);
}
}
while(!s.isEmpty()) {
sb.append(s.pop());
}
System.out.println(sb.toString());
}
br.close();
}
}