The first line gives the number of songs n, and the second line gives the n song names of the playlist in order, separated by single spaces. The third line gives the number of commands q, and each of the next q lines gives one command. Handle the commands in order from the top. move i j takes out the i-th song from the front, then puts it back so that it becomes the j-th song from the front (positions count from 1). It can be done only when i and j are both at least 1 and at most the number of songs before this command (if i and j are equal, the list stays the same). remove s removes the first song s in the list. It cannot be done if the list has no s. reverse reverses the order of the whole list. It can be done even when the list is empty. Skip any command that cannot be done, and count the skipped commands. When stop appears, finish without handling or counting any later command. On the first line print the final playlist separated by single spaces (- if it is empty), and on the second line print the number of skipped commands.
Input
1 ≤ n ≤ 2000, 1 ≤ q ≤ 2000. Song names are 1 to 10 lowercase English letters or digits, and the same name may appear several times. i and j of move are integers from −10 to 10,000
Output
Two lines: the final playlist (- if empty) and the number of skipped commands
Example 1 · stdin
5
intro rain sun moon outro
8
move 1 3
remove sun
remove star
reverse
move 4 1
move 0 2
stop
reverse
stdout
rain outro moon intro
2
Example 2 · stdin
2
x y
4
remove x
remove y
remove x
reverse
stdout
-
1
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
For each command, count it and skip (continue) when it cannot be done, and stop (break) on stop. The first trap is break inside switch: in C, C++, Java, C#, JS and TS, a break inside switch only leaves the switch and the loop keeps going (Craykon Break (그만) inside switch (선택) is the same). So ask about stop with an if first and break. continue, on the other hand, goes to the next loop iteration even from inside a switch. The second trap is what reversing returns. Python songs.reverse() reverses in place and returns None, so songs = songs.reverse() loses the list (use songs[::-1] if you need a new one). JS reverse() reverses in place and returns the same array. Java Collections.reverse(songs), C# List<T>.Reverse(), C++ std::reverse(begin, end) and Craykon songs.Reverse() (songs.뒤집기()) also reverse in place, and in C you swap from both ends. The third trap is the order inside move: take out first (pop(i - 1), splice(i - 1, 1), Java remove(int), C# RemoveAt, Craykon RemoveAt (자리빼기)) and then insert (insert, splice(j - 1, 0, x), add(j - 1, x), Insert, Insert (끼우기)), so that j means "the position after taking out". The fourth trap is the range check: in Python pop(-1) removes the last item, so i = 0 silently moves the wrong song; always check that it is at least 1. Java has both remove(int) (position) and remove(Object) (value).
Pythonmain.py · 25 lines
input()songs = input().split()q = int(input())skipped = 0for _ in range(q): cmd, *arg = input().split() if cmd == "stop": break # 남은 명령은 읽지도 세지도 않는다 if cmd == "reverse": songs.reverse() # 제자리에서 뒤집는다(돌려주는 값은 None) elif cmd == "remove": if arg[0] not in songs: skipped += 1 continue songs.remove(arg[0]) # 맨 앞의 것 하나 else: i, j = int(arg[0]), int(arg[1]) # 0이나 음수를 그대로 쓰면 Python은 뒤에서부터 세어 버린다 if not (1 <= i <= len(songs) and 1 <= j <= len(songs)): skipped += 1 continue song = songs.pop(i - 1) songs.insert(j - 1, song) # 뺀 뒤의 목록에서 j번째 자리print(" ".join(songs) if songs else "-")print(skipped)
JavaScriptmain.js · 28 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const songs = lines[1].trim().split(" ");const q = Number(lines[2]);let skipped = 0;for (const line of lines.slice(3, 3 + q)) { const [cmd, a, b] = line.trim().split(" "); if (cmd === "stop") break; // 남은 명령은 세지 않는다 if (cmd === "reverse") { songs.reverse(); // 제자리에서 뒤집는다 } else if (cmd === "remove") { const at = songs.indexOf(a); if (at === -1) { skipped++; continue; } songs.splice(at, 1); } else { const i = Number(a), j = Number(b); if (!(1 <= i && i <= songs.length && 1 <= j && j <= songs.length)) { skipped++; continue; } const [song] = songs.splice(i - 1, 1); // 먼저 빼고 songs.splice(j - 1, 0, song); // 뺀 뒤의 목록에서 j번째 자리에 끼운다 }}console.log(songs.length > 0 ? songs.join(" ") : "-");console.log(skipped);
TypeScriptmain.ts · 29 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const songs: string[] = lines[1].trim().split(" ");const q = Number(lines[2]);let skipped = 0;for (const line of lines.slice(3, 3 + q)) { const [cmd, a, b] = line.trim().split(" "); if (cmd === "stop") break; // 남은 명령은 세지 않는다 if (cmd === "reverse") { songs.reverse(); // 제자리에서 뒤집는다 } else if (cmd === "remove") { const at = songs.indexOf(a); if (at === -1) { skipped++; continue; } songs.splice(at, 1); } else { const i = Number(a), j = Number(b); if (!(1 <= i && i <= songs.length && 1 <= j && j <= songs.length)) { skipped++; continue; } const [song] = songs.splice(i - 1, 1); // 먼저 빼고 songs.splice(j - 1, 0, song); // 뺀 뒤의 목록에서 j번째 자리에 끼운다 }}console.log(songs.length > 0 ? songs.join(" ") : "-");console.log(skipped);
JavaMain.java · 34 lines
import java.io.*;import java.util.*;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.readLine(); List<String> songs = new ArrayList<>(Arrays.asList(br.readLine().trim().split(" "))); int q = Integer.parseInt(br.readLine().trim()); int skipped = 0; for (int t = 0; t < q; t++) { String[] p = br.readLine().trim().split(" "); if (p[0].equals("stop")) break; // switch 안의 break는 switch만 빠져나가서 if로 쓴다 if (p[0].equals("reverse")) { Collections.reverse(songs); } else if (p[0].equals("remove")) { if (!songs.remove(p[1])) { // remove(Object): 맨 앞의 것 하나, 없으면 false skipped++; continue; } } else { int i = Integer.parseInt(p[1]), j = Integer.parseInt(p[2]); if (i < 1 || i > songs.size() || j < 1 || j > songs.size()) { skipped++; continue; } String song = songs.remove(i - 1); // remove(int): 그 자리의 것 songs.add(j - 1, song); } } System.out.println(songs.isEmpty() ? "-" : String.join(" ", songs)); System.out.println(skipped); }}
C#Main.cs · 44 lines
using System;using System.Collections.Generic;class Program{ static void Main() { Console.ReadLine(); var songs = new List<string>(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)); int q = int.Parse(Console.ReadLine().Trim()); int skipped = 0; for (int t = 0; t < q; t++) { string[] p = Console.ReadLine().Trim().Split(' '); if (p[0] == "stop") break; // switch 안의 break는 switch만 빠져나가서 if로 쓴다 if (p[0] == "reverse") { songs.Reverse(); // List<T>.Reverse()는 제자리에서 뒤집는다 } else if (p[0] == "remove") { if (!songs.Remove(p[1])) { skipped++; continue; } } else { int i = int.Parse(p[1]), j = int.Parse(p[2]); if (i < 1 || i > songs.Count || j < 1 || j > songs.Count) { skipped++; continue; } string song = songs[i - 1]; songs.RemoveAt(i - 1); songs.Insert(j - 1, song); } } Console.WriteLine(songs.Count > 0 ? string.Join(" ", songs) : "-"); Console.WriteLine(skipped); }}
C++main.cpp · 44 lines
#include <algorithm>#include <iostream>#include <sstream>#include <string>#include <vector>int main() { int n, q; std::cin >> n; std::vector<std::string> songs(n); for (auto& s : songs) std::cin >> s; std::cin >> q; int skipped = 0; for (int t = 0; t < q; t++) { std::string cmd; std::cin >> cmd; if (cmd == "stop") break; // 남은 명령은 세지 않는다 if (cmd == "reverse") { std::reverse(songs.begin(), songs.end()); } else if (cmd == "remove") { std::string w; std::cin >> w; auto it = std::find(songs.begin(), songs.end(), w); if (it == songs.end()) { skipped++; continue; } songs.erase(it); } else { int i, j, len = songs.size(); std::cin >> i >> j; if (i < 1 || i > len || j < 1 || j > len) { skipped++; continue; } std::string song = songs[i - 1]; songs.erase(songs.begin() + (i - 1)); songs.insert(songs.begin() + (j - 1), song); } } if (songs.empty()) std::cout << "-"; for (size_t k = 0; k < songs.size(); k++) std::cout << (k ? " " : "") << songs[k]; std::cout << "\n" << skipped << "\n";}
Cmain.c · 64 lines
#include <stdio.h>#include <string.h>static char songs[2000][11];static int len;/* 자리 at의 노래를 빼고 뒤를 한 칸씩 당긴다 */static void remove_at(int at) { memmove(songs[at], songs[at + 1], sizeof(songs[0]) * (len - at - 1)); len--;}/* 자리 at에 끼우고 뒤를 한 칸씩 민다 */static void insert_at(int at, const char *s) { memmove(songs[at + 1], songs[at], sizeof(songs[0]) * (len - at)); strcpy(songs[at], s); len++;}int main(void) { int q, skipped = 0; scanf("%d", &len); for (int i = 0; i < len; i++) scanf("%10s", songs[i]); scanf("%d", &q); for (int t = 0; t < q; t++) { char cmd[8], w[11]; scanf("%7s", cmd); /* switch 안의 break는 switch만 빠져나가니 멈추기는 if로 쓴다 */ if (strcmp(cmd, "stop") == 0) break; if (strcmp(cmd, "reverse") == 0) { for (int a = 0, b = len - 1; a < b; a++, b--) { char tmp[11]; strcpy(tmp, songs[a]); strcpy(songs[a], songs[b]); strcpy(songs[b], tmp); } } else if (strcmp(cmd, "remove") == 0) { scanf("%10s", w); int at = -1; for (int k = 0; k < len; k++) if (strcmp(songs[k], w) == 0) { at = k; break; } if (at < 0) { skipped++; continue; } remove_at(at); } else { int i, j; scanf("%d %d", &i, &j); if (i < 1 || i > len || j < 1 || j > len) { skipped++; continue; } char song[11]; strcpy(song, songs[i - 1]); remove_at(i - 1); insert_at(j - 1, song); } } if (len == 0) printf("-"); for (int k = 0; k < len; k++) printf(k ? " %s" : "%s", songs[k]); printf("\n%d\n", skipped); return 0;}
Craykonmain.cray · 48 lines
Console.ReadLine();songs = Console.ReadLine().Trim().Split();q = Console.ReadLine().ToNumber();skipped = 0;for (t = 0; t < q; t++){ p = Console.ReadLine().Trim().Split(); cmd = p[0]; // 선택 안의 그만은 선택만 빠져나가니 멈추기는 만약으로 쓴다 if (cmd == "stop") { break; } if (cmd == "reverse") { songs.Reverse(); } else if (cmd == "remove") { if (!songs.Remove(p[1])) { skipped++; continue; } } else { i = p[1].ToNumber(); j = p[2].ToNumber(); if (i < 1 || i > songs.Count || j < 1 || j > songs.Count) { skipped++; continue; } song = songs[i - 1]; songs.RemoveAt(i - 1); songs.Insert(j - 1, song); }}if (songs.Count > 0){ Console.WriteLine(songs.Join(" "));}else{ Console.WriteLine("-");}Console.WriteLine(skipped);