ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

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

 

20437๋ฒˆ: ๋ฌธ์ž์—ด ๊ฒŒ์ž„ 2

์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์—์„œ 3๋ฒˆ์—์„œ ๊ตฌํ•œ ๋ฌธ์ž์—ด์€ aqua, 4๋ฒˆ์—์„œ ๊ตฌํ•œ ๋ฌธ์ž์—ด์€ raquator์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์—์„œ๋Š” ์–ด๋–ค ๋ฌธ์ž๊ฐ€ 5๊ฐœ ํฌํ•จ๋œ ๋ฌธ์ž์—ด์„ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

[ํ’€์ด]

์–ด๋–ค ๋ฌธ์ž๋ฅผ a๋ผ๊ณ  ํ•œ๋‹ค๋ฉด ์ •ํ™•ํžˆ K๊ฐœ๋ฅผ ํฌํ•จํ•˜๋ฉฐ, ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๊ธ€์ž๊ฐ€ ๋ชจ๋‘ a์ธ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ for๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ดW์„ ์ˆœํšŒํ•˜๋ฉฐ K๊ฐœ ์ด์ƒ ํฌํ•จ๋œ ๋ฌธ์ž๋ฅผ ์ฐพ์•„์•ผํ•ฉ๋‹ˆ๋‹ค.

 

1. Map์„ ํ™œ์šฉํ•˜์—ฌ key = ๋ฌธ์ž , value = [๋ฌธ์ž์˜ ๊ฐœ์ˆ˜, index1, index2, ... ] ์˜ ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

2. ์™„์„ฑ๋œ Map์„ ์ˆœํšŒํ•˜๋ฉฐ value์˜ 0๋ฒˆ์งธ ๊ฐ’, ์ฆ‰ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๊ฐ€ K์ด์ƒ์ธ key๊ฐ’์— ๋Œ€ํ•ด์„œ๋งŒ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

 

3. value์˜ 1๋ฒˆ์งธ๊ฐ’~๋งˆ์ง€๋ง‰๊ฐ’ ๊นŒ์ง€ ๋ฌธ์ž์˜ ์œ„์น˜๋“ค์ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— K๊ฐœ์”ฉ ๋ฌถ์–ด์„œ ๊ธธ์ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

 

4. ๊ตฌํ•ด์ง„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋“ค์„ words๋ฐฐ์—ด์— ๋„ฃ๊ณ , sort์—ฐ์‚ฐ์„ ํ†ตํ•ด ์ •๋ ฌํ•ด ๊ฐ€์žฅ ์งง์€ ๊ฐ’๊ณผ ๊ฐ€์žฅ ๊ธด ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ, words์˜ ๋ฐฐ์—ด์ด ๋น„์–ด์žˆ๋‹ค๋ฉด ๋งŒ๋“ค์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— -1์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

 

1. ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์˜ˆ์ œ 1๋ฒˆ์˜ T1์„ ๊ตฌํ•ด๋ณด๋ฉด Map์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด = superaquatornado
K = 2

Map(12) {
  's' => [ 1, 0 ],
  'u' => [ 2, 1, 7 ],
  'p' => [ 1, 2 ],
  'e' => [ 1, 3 ],
  'r' => [ 2, 4, 11 ],
  'a' => [ 3, 5, 8, 13 ],
  'q' => [ 1, 6 ],
  't' => [ 1, 9 ],
  'o' => [ 2, 10, 15 ],
  'n' => [ 1, 12 ],
  'd' => [ 1, 14 ]
}

 

2. Map์„ ์ˆœํšŒํ•˜๋ฉฐ K๊ฐœ ์ด์ƒ ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ’์„ ๋ฝ‘์•„๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

Map(12) {
  'u' => [ 2, 1, 7 ]
  'r' => [ 2, 4, 11 ],
  'a' => [ 3, 5, 8, 13 ]
  'o' => [ 2, 10, 15 ]
}

 

3. K๊ฐœ๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ value๋ฐฐ์—ด์˜ 1๋ฒˆ์งธ ์ดํ›„ index๋“ค์„ K ๊ฐ„๊ฒฉ์œผ๋กœ ๋นผ์„œ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 'u'๋Š” [1, 7] ์— ์œ„์น˜ํ•˜๋ฏ€๋กœ ๊ธธ์ด๋Š” 7 - 1 + 1 = 6์ž…๋‹ˆ๋‹ค.

 

4. ๊ตฌํ•ด์ง„ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ €์žฅํ•œ ๋ฐฐ์—ด words์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

[ 4, 6, 6, 7, 8 ]

 

๋”ฐ๋ผ์„œ ์ตœ์†Œ๊ฐ’ 4, ์ตœ๋Œ€๊ฐ’ 8์„ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

const fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().split('\n');

const T = Number.parseInt(input[0]);

const findWord = (W, K) => {
    const map = new Map();
    const words = [];

    for(let i=0;i<W.length;i++) {
        if(!map.has(W[i])) {
            map.set(W[i], [1, i]);
        } else {
            const arr = [...map.get(W[i]), i];
            arr[0] += 1;
            map.set(W[i], arr);
        }
    }

    map.forEach((v, k) => {
        if(v[0] >= K) {
            for(i=v.length-1;i>=K;i--) {
                words.push(v[i] - v[i-K+1] + 1);
            }
        }
    });
    if(words.length === 0) {
        console.log(-1);
    } else {
        words.sort((a,b) => a-b);
        console.log(words[0] + " " + words[words.length-1]);
    }
}

for(let i=1;i<=T*2;i+=2) {
    const W = input[i];
    const K = Number.parseInt(input[i+1]);
    findWord(W,K);
}