새소식

알고리즘/문제

[BOJ/백준 - 1046] 에디터

  • -

문제 출처:https://www.acmicpc.net/problem/1406

 

1406번: 에디터

첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수

www.acmicpc.net


public class BOJ_1406 {
static char[] arr = new char[100002];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer str = new StringBuffer(br.readLine());
char[] cArr = str.toString().toCharArray();
int testCase = Integer.parseInt(br.readLine());
StringTokenizer st;
Deque<Character> left = new LinkedList<>();
Deque<Character> right = new LinkedList<>();
for(int i=0; i<cArr.length; i++){
left.add(cArr[i]);
}
while(testCase > 0){
st = new StringTokenizer(br.readLine());
String status = st.nextToken();
if("L".equals(status)){
if(!left.isEmpty()){
right.addFirst(left.removeLast());
}
}else if("D".equals(status)){
if(!right.isEmpty()){
left.addLast(right.removeFirst());
}
}else if("B".equals(status)){
if(!left.isEmpty()){
left.removeLast();
}
}else{
left.addLast(st.nextToken().charAt(0));
}
testCase--;
}
StringBuilder answer = new StringBuilder();
left.forEach((c)->{answer.append(c);});
right.forEach((c)->{answer.append(c);});
System.out.print(answer);
}
}
view raw BOJ_1406.java hosted with ❤ by GitHub

Deqeue를 2개 만들어 현재 커서 위치 기준으로

왼쪽 데이터, 오른쪽 데이터를 나눠서

문제의 조건대로 문자를 왓다리 갓다리 하면 된당

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.