fork download
  1. import java.util.LinkedList
  2. import java.util.StringTokenizer
  3.  
  4. fun main() {
  5. val br = System.`in`.bufferedReader()
  6. val queue = LinkedList<Char>()
  7. val str = br.readLine()
  8. for (c in str) {
  9. queue.add(c)
  10. }
  11. val m = br.readLine().toInt()
  12.  
  13. var index = queue.size
  14. repeat(m) {
  15. val token = StringTokenizer(br.readLine())
  16. when (token.nextToken().first()) {
  17. 'L' -> { if (index > 0) index-- }
  18. 'D' -> { if (index < queue.size) index++ }
  19. 'B' -> {
  20. if (index > 0) {
  21. index--
  22. queue.removeAt(index)
  23. }
  24. }
  25. 'P' -> {
  26. queue.add(index, token.nextToken().first())
  27. index++
  28. }
  29. }
  30. println(queue)
  31. }
  32.  
  33. val sb = StringBuilder()
  34. for (c in queue) {
  35. sb.append(c)
  36. }
  37. println(sb)
  38. }
Success #stdin #stdout 0.08s 40948KB
stdin
abc
9
L
L
L
L
L
P x
L
B
P y
stdout
[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