Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class StudentsOrder {
- public static void testInput() {
- String res = "5 3\n" +
- "Gosho Tosho Penka Miro Stanka\n" +
- "Miro Gosho\n" +
- "Gosho Stanka\n" +
- "Stanka Miro";
- System.setIn(new ByteArrayInputStream(res.getBytes()));
- }
- public static void main(String[] args) throws Exception {
- // testInput();
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- String[] firstLine = input.readLine().split(" ");
- int n = Integer.parseInt(firstLine[0]);
- int k = Integer.parseInt(firstLine[1]);
- ArrayList<String> students = new ArrayList<>();
- String[] studentNames = input.readLine().split(" ");
- for (int i = 0; i < n; i++) {
- students.add(studentNames[i]);
- }
- for (int i = 0; i <k; i++) {
- String[] replacingNames = input.readLine().split(" ");
- String name1 = replacingNames[0];
- String name2 = replacingNames[1];
- int index = students.indexOf(name1);
- int indexC = students.indexOf(name2);
- if (indexC<index){
- if (indexC==0){
- students.add(0, name1);
- }else {
- students.add(indexC, name1);
- }
- students.remove(index + 1);
- }
- else {
- students.add(indexC,name1);
- students.remove(index );
- }
- }
- StringBuilder res = new StringBuilder();
- for (String student: students) {
- res.append(student);
- res.append(" ");
- }
- res.deleteCharAt(res.length()-1);
- System.out.println(res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement