medon3

Students Order2

Jan 15th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package AlgorithmsLinearDataStructures;//Students order
  2. //Alpha students love learning new stuff. They also know that in order to better understand and remember new stuff students should change their seats in the room. You are given the names of N Alpha students and K changes of seats. Your task is to implement an algorithm which displays the final ordering of the students after applying all seat changes.
  3. //
  4. //Input
  5. //Read from the standard input
  6. //On the first line, find the number N and K
  7. //N - students count
  8. //K - the count of seat changes
  9. //On the next line there will be N names
  10. //On the next K lines there will be pair of names separated by white space
  11. //The first name takes the seat next to the second name (first is left, second is right)
  12. //See sample tests for 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. //First Miro seats next to Gosho, so the order is - Miro Gosho Tosho Penka Stanka
  32. //Next Gosho seats next to Stanka, so the order is - Miro Tosho Penka Gosho Stanka
  33. //At last Stanka seats next to Miro, so the final order is 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. import java.util.Arrays;
  45. import java.util.LinkedList;
  46. import java.util.Scanner;
  47.  
  48. public class StudentsOrder {
  49.  
  50.  
  51. public static void main(String[] args) {
  52. Scanner scanner = new Scanner(System.in);
  53. String[] counts = scanner.nextLine().split(" ");
  54. int n = Integer.parseInt(counts[0]);
  55. int k = Integer.parseInt(counts[1]);
  56.  
  57. String[] students = scanner.nextLine().split(" ");
  58. LinkedList<String> studentsList = new LinkedList<>(Arrays.asList(students));
  59.  
  60. for (int i = 0; i < k; i++) {
  61. String[] change = scanner.nextLine().split(" ");
  62. studentsList.remove(change[0]);
  63. int insertPosition = studentsList.indexOf(change[1]);
  64. studentsList.add(insertPosition, change[0]);
  65. }
  66.  
  67. System.out.println(String.join(" ", studentsList));
  68. }
  69.  
  70. }
  71.  
  72. //EKA's
  73. // import java.util.*;
  74. //import java.util.stream.Collectors;
  75. //
  76. //public class Main {
  77. // public static void main(String[] args) {
  78. // Scanner scanner = new Scanner(System.in);
  79. // String[] inputLine = scanner.nextLine().split(" ");
  80. // int studentsCount = Integer.parseInt(inputLine[0]);
  81. // int countOfSeatsChanges = Integer.parseInt(inputLine[1]);
  82. // List<String> students = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  83. //
  84. // String[] ordered = new String[students.size()];
  85. //
  86. // for (int i = 0; i < countOfSeatsChanges; i++) {
  87. // if (students == null || students.size() == 1) {
  88. //
  89. // }
  90. // String[] nextPair = scanner.nextLine().split(" ");
  91. // String studentToTake = nextPair[0];
  92. // String sitInfrontOf = nextPair[1];
  93. // int tempStudent = students.indexOf(studentToTake);
  94. // students.remove(tempStudent);
  95. // int secondStudentIndex = students.indexOf(sitInfrontOf);
  96. // students.add(secondStudentIndex, studentToTake);
  97. //
  98. // }
  99. //
  100. // StringBuilder sb = new StringBuilder();
  101. // for (String student : students) {
  102. // sb.append(student).append(" ");
  103. // }
  104. // System.out.println(sb);
  105. // }
  106. //}
Advertisement
Add Comment
Please, Sign In to add comment