๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿ–ฅ dev-ruby

[๋ฐฑ์ค€] 5052 - ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก | Gold4 | node.js ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ณธ๋ฌธ

๋ฐฑ์ค€

[๋ฐฑ์ค€] 5052 - ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก | Gold4 | node.js ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

ruby_s 2022. 5. 8. 01:10
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฌธ์ œ

https://www.acmicpc.net/problem/5052

 

5052๋ฒˆ: ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ t๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ t ≤ 50) ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 10000) ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š” ๋ชฉ๋ก์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€

www.acmicpc.net

 

์ฝ”๋“œ

function solve(phoneNumList) {
  phoneNumList.sort();

  for (let i = 1; i < phoneNumList.length; i++) {
    const prevNum = phoneNumList[i - 1];
    const currNum = phoneNumList[i];
    if (currNum.startsWith(prevNum)) return "NO";
  }
  return "YES";
}

const filePath =
  process.platform === "linux" ? "/dev/stdin" : "๋ฐฑ์ค€/gold/5052/testcase.txt";
const input = require("fs")
  .readFileSync(filePath)
  .toString()
  .trim()
  .split("\n");
for (let i = 1; i < input.length; ) {
  const n = +input[i];
  console.log(solve(input.slice(i + 1, i + 1 + n)));
  i += n + 1;
}

์ฒ˜์Œ์—”  ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ž๋ฆฟ์ˆ˜๊ฐ€ ์ž‘์€ ์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์„ ํ˜• ํƒ์ƒ‰์œผ๋กœ ํ’€์—ˆ์œผ๋‚˜ ์—ญ์‹œ๋‚˜ ์‹œ๊ฐ„์ดˆ๊ณผ ..!

์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋ฌธ์ž์—ด ์ •๋ ฌ์€ "976 > 911867" ์ด๋Ÿฐ์‹์œผ๋กœ ํŒ๋‹จ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ sort๋งŒ ์จ๋„ ๋œ๋‹ค.
์•„๋ž˜ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ๋ฅผ ๋ณด๋ฉด,

์˜ˆ์ œ ์ž…๋ ฅ 1

976
97625999
91125426

์˜ˆ์ œ ์ถœ๋ ฅ 1

NO

์ •๋ ฌํ•˜๋ฉด ["91125426", "976", "97625999"] ์ด๋ ‡๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ i์˜ ํ˜„์žฌ ๊ฐ’๊ณผ ๋‹ค์Œ ๊ฐ’๋งŒ ๋น„๊ตํ•ด์ค˜๋„ ์ •๋‹ต์ด ๋‚˜์˜จ๋‹ค. ๊ตณ์ด ์„ ํ˜•ํƒ์ƒ‰์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๋น„๊ต๋ฅผ ๋‹ค ์•ˆํ•ด์ค˜๋„ ๋œ๋‹ค.

 

์–ด๋ฆฌ์„์€ ๋‚˜ ๋ฐ˜์„ฑํ•ด๋ผ๐Ÿ˜ง
728x90
๋ฐ˜์‘ํ˜•
LIST