sunfresh

Students order

Jan 15th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. Students order
  2. 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.
  3.  
  4. Input
  5. Read from the standard input.
  6. On the first line, you'll find the numbers N and K separated by a white space.
  7. N - the number of students.
  8. K - the number of seat changes.
  9. On the next line there will be N student names separated by a white space.
  10. On the next K lines there will be a pair of students separated by a white space.
  11. The first student sits on the left of the second student.
  12. See the sample tests for further explanation.
  13. Output
  14. Print on the standard output.
  15. On a single line, print the final order of the student names.
  16. Constraints
  17. 1 <= N <= 2000
  18. 1 <= K <= 100 000
  19. Each name contains only alphanumeric characters.
  20. All names are unique.
  21. Sample tests
  22. Input
  23. 5 3
  24. Gosho Tosho Penka Miro Stanka
  25. Miro Gosho
  26. Gosho Stanka
  27. Stanka Miro
  28. Output
  29. Stanka Miro Tosho Penka Gosho
  30. Explanation
  31. Miro takes a seat next to Gosho. The order becomes Miro Gosho Tosho Penka Stanka
  32. Gosho takes a seat next to Stanka. The order becomes Miro Tosho Penka Gosho Stanka
  33. Stanka takes a seat next to Miro. The order becomes Stanka Miro Tosho Penka Gosho
  34. Input
  35. 7 4
  36. Emo Misho Ivanka Ginka Vancho Stancho Sashka
  37. Emo Misho
  38. Misho Emo
  39. Misho Sashka
  40. Sashka Stancho
  41. Output
  42. Emo Ivanka Ginka Vancho Sashka Stancho Misho
  43.  
  44.  
  45. import java.util.*;
  46.  
  47. public class Main {
  48. public static void main(String[] args) {
  49. Scanner scanner = new Scanner(System.in);
  50. String[] input = scanner.nextLine().split(" ");
  51. int seatChanges = Integer.parseInt(input[1]);
  52. String[] names = scanner.nextLine().split(" ");
  53. ArrayList<String> list = new ArrayList<>(Arrays.asList(names));
  54. for (int i = 0; i < seatChanges; i++) {
  55. String[] changes = scanner.nextLine().split(" ");
  56. String firstName = changes[0];
  57. String secondName = changes[1];
  58.  
  59. list.remove(firstName);
  60. int index = list.indexOf(secondName);
  61. list.add(index, firstName);
  62. }
  63. for (String s :list) {
  64. System.out.printf("%s ", s);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment