[Java] ๋ฐฑ์ค 5430๋ฒ - AC
[๋ฌธ์ ]
https://www.acmicpc.net/problem/5430
[ํ์ด]
์๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ํ์ดํ์์ต๋๋ค.
https://girawhale.tistory.com/9
๋ฐฐ์ด์ ์ง์ ๋ค์ง๋ ๊ฒ์ด ์๋๋ผ, start์ end์ ์ ์ง์ ํด๋๊ณ ๋๊ฐ๋ฅผ swapํด์ฃผ๋ ๋ฐฉ๋ฒ์ผ๋ก ํ์ด์ค์ด์์ผ๋, ์๊พธ ๋ฐํ์ ์๋ฌ๊ฐ ๋์ ํ์ธํด๋ณด๋ ์ด๋ฐ์ ๋ฐฐ์ด์ ์ด๊ธฐํ ํ๋ ๊ณผ์ ์์ ์ซ์๋ฅผ ์๋ชป ๋ฝ์๋ด๊ณ ์์์ต๋๋ค.ใ ใ ๋ํ ํ์ด๋ฅผ ์ดํด๋ณด๋ Deque๋ฅผ ํ์ฉํ๋ค๋ฉด, ์ฝ๊ฒ ์ ๋ค๋ฅผ ์๋ผ๋ผ์ ์๋ค๋๊ฑธ ์๊ฒ๋์๊ณ Deque๋ฅผ ํ์ฉํ ํ์ด๋ฒ์ผ๋ก ์งํํ์์ต๋๋ค.
์ฐ์ Stringํ์์ผ๋ก ๋ค์ด์ค๋ ๋ฐฐ์ด๊ฐ์ ๋งจ์([),๋งจ๋ค(])๋ฅผ ์๋ผ๋ธ ํ, ","์ ๊ธฐ์ค์ผ๋ก split()ํ์ฌ ์ซ์๋ค์ ๋ฝ์๋ธ ํ Deque์ ์ ์ฅํ์์ต๋๋ค.
String arr = br.readLine();
Deque<Integer> deque = new LinkedList<>();
for (String s : arr.substring(1, arr.length() - 1).split(",")) {
if (!s.equals("")) {
deque.add(Integer.valueOf(s));
}
}
๋ค์์ผ๋ก ACํจ์๋ฅผ ํตํด ์ํํด์ผํ ๋์๋ค์ธ f๋ฅผ ํ์ํ๋ฉฐ R๊ณผ D๋ฅผ ์ํํ์์ต๋๋ค.
๋ง์ฝ R์ด ๋ค์ด์จ๋ค๋ฉด, ๊ฐ์ ์ญ์ ํ๋ ๊ธฐ์ค๊ฐ First์ Last๋ฅผ ๋ฐ๊ฟ์ฃผ์์ต๋๋ค. D๊ฐ ๋ค์ด์์๋ Deque๊ฐ ๋น์ด์๋ค๋ฉด error๋ฅผ ์ถ๋ ฅํ์๊ณ , ๋น์ด์์ง ์๋ค๋ฉด ๊ธฐ์ค๊ฐ์ด ๋ฐ๋ผ๋ณด๋ First๋ Last์์ ๊ฐ์ ์ ๊ฑฐํ์์ต๋๋ค.
[์ฝ๋]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
public class Main {
// R : ๋ค์ง๊ธฐ
// D : ์ฒซ๋ฒ์งธ ์ ๋ฒ๋ฆฌ๊ธฐ
public static StringBuffer sb = new StringBuffer();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int i=0;i<T;i++) {
String f = br.readLine();
int n = Integer.parseInt(br.readLine());
String arr = br.readLine();
Deque<Integer> deque = new LinkedList<>();
for (String s : arr.substring(1, arr.length() - 1).split(",")) {
if (!s.equals("")) {
deque.add(Integer.valueOf(s));
}
}
AC(deque, f);
}
}
public static void AC(Deque<Integer> deque, String f) {
boolean reverse = false;
for(int i=0;i<f.length();i++) {
if(f.charAt(i) == 'R') {
reverse = !reverse;
} else {
if(deque.size() == 0) {
System.out.println("error");
return;
}
if(reverse) {
deque.removeLast();
} else {
deque.removeFirst();
}
}
}
StringBuilder s = new StringBuilder("[");
while(!deque.isEmpty()) {
s.append(reverse ? deque.removeLast() : deque.removeFirst());
if(deque.size() != 0) {
s.append(",");
}
}
s.append(']');
System.out.println(s);
}
}