Advertisement
Alekss33

Untitled

May 1st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5.  
  6. public class StudentsOrder {
  7.  
  8.  
  9. public static void testInput() {
  10. String res = "5 3\n" +
  11. "Gosho Tosho Penka Miro Stanka\n" +
  12. "Miro Gosho\n" +
  13. "Gosho Stanka\n" +
  14. "Stanka Miro";
  15. System.setIn(new ByteArrayInputStream(res.getBytes()));
  16. }
  17.  
  18. public static void main(String[] args) throws Exception {
  19.  
  20. // testInput();
  21. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  22. String[] firstLine = input.readLine().split(" ");
  23. int n = Integer.parseInt(firstLine[0]);
  24. int k = Integer.parseInt(firstLine[1]);
  25.  
  26. ArrayList<String> students = new ArrayList<>();
  27. String[] studentNames = input.readLine().split(" ");
  28. for (int i = 0; i < n; i++) {
  29. students.add(studentNames[i]);
  30. }
  31.  
  32.  
  33. for (int i = 0; i <k; i++) {
  34. String[] replacingNames = input.readLine().split(" ");
  35. String name1 = replacingNames[0];
  36. String name2 = replacingNames[1];
  37. int index = students.indexOf(name1);
  38. int indexC = students.indexOf(name2);
  39.  
  40. if (indexC<index){
  41. if (indexC==0){
  42. students.add(0, name1);
  43. }else {
  44. students.add(indexC, name1);
  45. }
  46. students.remove(index + 1);
  47.  
  48. }
  49. else {
  50. students.add(indexC,name1);
  51. students.remove(index );
  52.  
  53. }
  54.  
  55.  
  56. }
  57. StringBuilder res = new StringBuilder();
  58. for (String student: students) {
  59. res.append(student);
  60. res.append(" ");
  61. }
  62. res.deleteCharAt(res.length()-1);
  63. System.out.println(res);
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement