Baekjoon

[Baekjoon] js 24431 유사 라임 게임

도옹건 2024. 3. 22. 17:24

문제

 

해결과정

Map을 통해 공통 접미사의 개수를 저장하고 개수가 2개 이상이면 1쌍이 나오니 2로 나누어 쌍의 개수를 구하였다.

 

ex) L = 2이고,  WALK, TALK, MILK, BULK이면 LK의 개수가 4개이니 총 2쌍이 나온다.

 

전체 코드

const fs = require("fs");
const input = fs
  .readFileSync("/dev/stdin")
  .toString()
  .trim()
  .split("\n")
  .map((v) => v.trim());

const t = +input.shift();
const result = [];

for (let i = 0; i < t; i++) {
  let count = 0;
  const map = new Map();
  const [n, l, f] = input.shift().split(" ");
  const arr = input.shift().split(" ");
  arr.forEach((value) => {
    const str = value.slice(-f);
    map.set(str, (map.get(str) || 0) + 1);
  });

  map.forEach((value, _) => {
    if (value >= 2) {
      count += Math.floor(value / 2);
    }
  });

  result.push(count);
}

console.log(result.join("\n"));

 

문제 링크 : https://www.acmicpc.net/problem/24431