05-18 01:37
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자바
- descriptive statistics
- Entity
- Java
- Binary Search
- 셀레니움
- Class
- Reference Type
- 백준
- spring
- External Scheme
- dbms
- selenium
- OOP
- X.org
- Physical Scheme
- BFS
- Operator
- Unity
- 리눅스
- literal
- python
- X윈도우
- Polymolphism
- Entity Set
- Inheritance
- Mac
- systemd
- preprocessing
- 리눅스 마스터 1급
Archives
- Today
- Total
Byeol Lo
N24511 - queuestack 본문
원래 의도는 다음과 같은 자료구조를 구현하는 것이다.
package stack;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.LinkedList;
public class N24511 {
final static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
final static StringBuffer SB = new StringBuffer();
static StringTokenizer st;
public static void main(String[] args) throws IOException {
BR.readLine();
ArrayList<Deque<Integer>> sq = new ArrayList<>();
ArrayList<Integer> checkSQ = new ArrayList<>();
input(checkSQ);
solution(sq, checkSQ);
System.out.println(SB);
BR.close();
}
private static void input(ArrayList<Integer> checkSQ) throws IOException {
st = new StringTokenizer(BR.readLine());
while(st.hasMoreTokens())
checkSQ.add(Integer.parseInt(st.nextToken()));
}
private static void solution(
ArrayList<Deque<Integer>> sq,
ArrayList<Integer> checkSQ
) throws IOException {
int tmp;
st = new StringTokenizer(BR.readLine());
while(st.hasMoreTokens()) {
Deque<Integer> d = new LinkedList<>();
d.add(Integer.parseInt(st.nextToken()));
sq.add(d);
}
BR.readLine();
st = new StringTokenizer(BR.readLine());
while(st.hasMoreTokens()) {
tmp = Integer.parseInt(st.nextToken());
for(int i=0; i<checkSQ.size(); i++) {
sq.get(i).offerLast(tmp);
tmp = (checkSQ.get(i) == 0) ?
sq.get(i).pollFirst() :
sq.get(i).pollLast();
}
SB.append(tmp).append(" ");
}
}
}
하지만 시간제한이 있기 때문에 queue, stack의 성질을 알고 이를 간소화시키는 것이다. 따라서 다음과 같다.
package stack;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.LinkedList;
public class N24511 {
final static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
final static StringBuffer SB = new StringBuffer();
static StringTokenizer st;
public static void main(String[] args) throws IOException {
solution();
BR.close();
}
private static void solution() throws IOException {
BR.readLine();
st = new StringTokenizer(BR.readLine());
ArrayList<Integer> checkSQ = new ArrayList<>();
Deque<Integer> queue = new LinkedList<>();
while(st.hasMoreTokens())
checkSQ.add(Integer.parseInt(st.nextToken()));
st = new StringTokenizer(BR.readLine());
for(int i : checkSQ)
if(i == 0) queue.offerLast(Integer.parseInt(st.nextToken()));
else st.nextToken();
BR.readLine();
st = new StringTokenizer(BR.readLine());
while(st.hasMoreTokens()) {
queue.offerFirst(Integer.parseInt(st.nextToken()));
SB.append(queue.pollLast()).append(" ");
}
System.out.println(SB);
}
}
구현 능력을 따지려면 첫번째 풀이가, stack과 queue의 특성을 잘 알고 있는지 물어보는 것은 후자가 더 나은거 같다. 이 또한 dequeue를 적절히 활용할 수 있는지 묻는 것 같다.
'Algorithm > Stack & Queue' 카테고리의 다른 글
N2346 - 풍선 터뜨리기 (0) | 2023.12.09 |
---|---|
N28279 - 덱 2 (1) | 2023.12.08 |
N12789 - 도키도키 간식드리미 (0) | 2023.12.08 |
N28278 - 스택 2 (0) | 2023.12.08 |
Comments