import java.util.LinkedList import java.util.StringTokenizer fun main() { val br = System.`in`.bufferedReader() val queue = LinkedList<Char>() val str = br.readLine() for (c in str) { queue.add(c) } val m = br.readLine().toInt() var index = queue.size repeat(m) { val token = StringTokenizer(br.readLine()) when (token.nextToken().first()) { 'L' -> { if (index > 0) index-- } 'D' -> { if (index < queue.size) index++ } 'B' -> { if (index > 0) { index-- queue.removeAt(index) } } 'P' -> { queue.add(index, token.nextToken().first()) index++ } } println(queue) } val sb = StringBuilder() for (c in queue) { sb.append(c) } println(sb) }
abc 9 L L L L L P x L B P y
[a, b, c] [a, b, c] [a, b, c] [a, b, c] [a, b, c] [x, a, b, c] [x, a, b, c] [x, a, b, c] [y, x, a, b, c] yxabc