Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
1
Indexable
Never
// let input = [
//     '7 4',
//     'Emo Misho Ivanka Ginka Vancho Stancho Sashka',
//     'Emo Misho',
//     'Misho Emo',
//     'Misho Sashka',
//     'Sashka Stancho',
// ];
// Expected output: Emo Ivanka Ginka Vancho Sashka Stancho Misho

let input = [
  '5 3',
  'Gosho Tosho Penka Miro Stanka',
  'Miro Gosho',
  'Gosho Stanka',
  'Stanka Miro',
];
// Stanka Miro Tosho Penka Gosho

const gets = ((array, index) => () => array[index++])(input, 0);

let [N, K] = gets().split(' ');

if (N < 1 || N > 2000) {
  throw new Error();
}

if (K < 1 || K > 100000) {
  throw new Error();
}

const students = gets().split(' ');

for (let i = 0; i < K; i++) {
  const [left, right] = gets().split(' ');

  const foundLeft = students.indexOf(left);
  const foundRight = students.indexOf(right);

  if (foundLeft === -1 || foundRight === -1 || foundLeft + 1 === foundRight) {
    continue;
  }

  students.splice(foundLeft, 1);
  students.splice(foundRight === 0 || foundRight < foundLeft ? foundRight : foundRight - 1, 0, left);
}

print(students.join(' '));