编辑代码

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  // 读取输入数组和输出数组,并转换为数字数组
  const inputArr = (await readline()).split(",").map(Number);
  const outputArr = (await readline()).split(",").map(Number);

  // 存储操作序列(L表示从左取出,R表示从右取出)
  let res = [];
  // 模拟双端队列,用于存储待处理的数字
  const queue = [];
  // 记录输出数组当前处理到的位置
  let idx = 0;

  // 遍历输入数组的每个数字
  for (const input of inputArr) {
    // 将当前数字加入队列
    queue.push(input);

    // 尝试从队列中取出数字,直到无法匹配或队列为空
    while (idx < outputArr.length && queue.length > 0) {
      // 获取队列最左边的数字
      const left = queue[0];
      // 获取队列最右边的数字
      const right = queue[queue.length - 1];

      // 如果最左边的数字匹配输出数组当前数字
      if (outputArr[idx] == left) {
        queue.shift(); // 从左边取出数字
        res.push("L"); // 记录操作
        idx++; // 移动到输出数组的下一个位置
      }
      // 如果最右边的数字匹配输出数组当前数字
      else if (right == outputArr[idx]) {
        queue.pop(); // 从右边取出数字
        res.push("R"); // 记录操作
        idx++; // 移动到输出数组的下一个位置
      }
      // 如果都不匹配,退出当前循环
      else break;
    }
  }

  // 如果操作序列长度小于输出数组长度,说明无法得到目标序列
  if (res.length < outputArr.length) return console.log("NO");

  // 输出操作序列
  console.log(res.join(""));
})();