Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Students order
- Alpha students love learning new stuff. They are also aware that switching seats in the classroom helps them learn new material more effectively. You are given the names of N students and K seat changes. Your task is to implement an algorithm that shows the students' final seating order after all seat changes have been made.
- Input
- Read from the standard input.
- On the first line, you'll find the numbers N and K separated by a white space.
- N - the number of students.
- K - the number of seat changes.
- On the next line there will be N student names separated by a white space.
- On the next K lines there will be a pair of students separated by a white space.
- The first student sits on the left of the second student.
- See the sample tests for further explanation.
- Output
- Print on the standard output.
- On a single line, print the final order of the student names.
- Constraints
- 1 <= N <= 2000
- 1 <= K <= 100 000
- Each name contains only alphanumeric characters.
- All names are unique.
- Sample tests
- Input
- 5 3
- Gosho Tosho Penka Miro Stanka
- Miro Gosho
- Gosho Stanka
- Stanka Miro
- Output
- Stanka Miro Tosho Penka Gosho
- Explanation
- Miro takes a seat next to Gosho. The order becomes Miro Gosho Tosho Penka Stanka
- Gosho takes a seat next to Stanka. The order becomes Miro Tosho Penka Gosho Stanka
- Stanka takes a seat next to Miro. The order becomes Stanka Miro Tosho Penka Gosho
- Input
- 7 4
- Emo Misho Ivanka Ginka Vancho Stancho Sashka
- Emo Misho
- Misho Emo
- Misho Sashka
- Sashka Stancho
- Output
- Emo Ivanka Ginka Vancho Sashka Stancho Misho
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(" ");
- int seatChanges = Integer.parseInt(input[1]);
- String[] names = scanner.nextLine().split(" ");
- ArrayList<String> list = new ArrayList<>(Arrays.asList(names));
- for (int i = 0; i < seatChanges; i++) {
- String[] changes = scanner.nextLine().split(" ");
- String firstName = changes[0];
- String secondName = changes[1];
- list.remove(firstName);
- int index = list.indexOf(secondName);
- list.add(index, firstName);
- }
- for (String s :list) {
- System.out.printf("%s ", s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment