Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const readline = require("readline");
  2.  
  3. function solve(lines) {
  4.   let S = lines.shift().split("").map(Number);
  5.  
  6.   S.push(0);
  7.  
  8.   let max = 0;
  9.   const res = [];
  10.  
  11.   for (let i = 0; i < S.length; i++) {
  12.     const mem = S[i];
  13.  
  14.     if (mem > max) {
  15.       for (let j = 0; j < mem - max; j++) {
  16.         res.push("(");
  17.       }
  18.     } else if (mem < max) {
  19.       for (let j = 0; j < max - mem; j++) {
  20.         res.push(")");
  21.       }
  22.     }
  23.  
  24.     max = mem;
  25.     res.push(mem);
  26.   }
  27.  
  28.   // pop 0 out
  29.   res.pop();
  30.  
  31.   process.stdout.write(`${res.join("")}\n`);
  32. }
  33.  
  34. // **** UTILS **** //
  35.  
  36. function readRawLines() {
  37.   return new Promise((resolve) => {
  38.     const rl = readline.createInterface({
  39.       input: process.stdin,
  40.       output: process.stdout,
  41.       terminal: false,
  42.     });
  43.  
  44.     const rawLines = [];
  45.  
  46.     rl.on("line", function (line) {
  47.       rawLines.push(line);
  48.     });
  49.  
  50.     rl.on("close", () => {
  51.       resolve(rawLines);
  52.     });
  53.   });
  54. }
  55.  
  56. (async function main() {
  57.   const lines = await readRawLines();
  58.  
  59.   const N = +lines.shift();
  60.  
  61.   for (let caseNo = 1; caseNo <= N; caseNo++) {
  62.     process.stdout.write(`Case #${caseNo}: `);
  63.     solve(lines);
  64.   }
  65. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement