n, k = map(int, input().split())songs = input().split()counts = {}once = 0 # 지금 구간에서 딱 한 번 나온 곡의 수out = []for i, song in enumerate(songs): # 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 c = counts.get(song, 0) + 1 counts[song] = c if c == 1: once += 1 elif c == 2: once -= 1 if i >= k: # 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 old = songs[i - k] c = counts[old] - 1 counts[old] = c if c == 1: once += 1 elif c == 0: once -= 1 if i >= k - 1: out.append(once)print(" ".join(map(str, out)))
JavaScriptmain.js · 24 行
const [nk, line] = require("fs").readFileSync(0, "utf8").split("\n");const [n, k] = nk.trim().split(" ").map(Number);const songs = line.trim().split(" ");// 객체 {}는 "constructor" 같은 이름을 이미 갖고 있다. Map은 넣은 열쇠만 갖는다const counts = new Map();let once = 0; // 지금 구간에서 딱 한 번 나온 곡의 수const out = [];for (let i = 0; i < n; i++) { // 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 const c = (counts.get(songs[i]) ?? 0) + 1; counts.set(songs[i], c); if (c === 1) once++; else if (c === 2) once--; if (i >= k) { // 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 const old = songs[i - k]; const d = counts.get(old) - 1; counts.set(old, d); if (d === 1) once++; else if (d === 0) once--; } if (i >= k - 1) out.push(once);}console.log(out.join(" "));
TypeScriptmain.ts · 25 行
import { readFileSync } from "node:fs";const [nk, line] = readFileSync(0, "utf8").split("\n");const [n, k] = nk.trim().split(" ").map(Number);const songs: string[] = line.trim().split(" ");// 객체 {}는 "constructor" 같은 이름을 이미 갖고 있다. Map은 넣은 열쇠만 갖는다const counts = new Map<string, number>();let once = 0; // 지금 구간에서 딱 한 번 나온 곡의 수const out: number[] = [];for (let i = 0; i < n; i++) { // 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 const c = (counts.get(songs[i]) ?? 0) + 1; counts.set(songs[i], c); if (c === 1) once++; else if (c === 2) once--; if (i >= k) { // 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 const old = songs[i - k]; const d = (counts.get(old) ?? 0) - 1; counts.set(old, d); if (d === 1) once++; else if (d === 0) once--; } if (i >= k - 1) out.push(once);}console.log(out.join(" "));
JavaMain.java · 36 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import java.util.StringJoiner;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] nk = br.readLine().trim().split(" "); int n = Integer.parseInt(nk[0]); int k = Integer.parseInt(nk[1]); String[] songs = br.readLine().trim().split(" "); // get은 없는 열쇠에 null을 돌려준다. getOrDefault로 0을 받는다 Map<String, Integer> counts = new HashMap<>(); int once = 0; // 지금 구간에서 딱 한 번 나온 곡의 수 StringJoiner out = new StringJoiner(" "); for (int i = 0; i < n; i++) { // 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 int c = counts.getOrDefault(songs[i], 0) + 1; counts.put(songs[i], c); if (c == 1) once++; else if (c == 2) once--; if (i >= k) { // 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 String old = songs[i - k]; int d = counts.get(old) - 1; counts.put(old, d); if (d == 1) once++; else if (d == 0) once--; } if (i >= k - 1) out.add(Integer.toString(once)); } System.out.println(out); }}
C#Main.cs · 36 行
using System;using System.Collections.Generic;class Program{ static void Main() { string[] nk = Console.ReadLine().Trim().Split(' '); int n = int.Parse(nk[0]); int k = int.Parse(nk[1]); string[] songs = Console.ReadLine().Trim().Split(' '); // counts[없는 열쇠]를 읽으면 KeyNotFoundException이 난다. GetValueOrDefault는 0을 준다 var counts = new Dictionary<string, int>(); int once = 0; // 지금 구간에서 딱 한 번 나온 곡의 수 var outList = new List<int>(); for (int i = 0; i < n; i++) { // 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 int c = counts.GetValueOrDefault(songs[i]) + 1; counts[songs[i]] = c; if (c == 1) once++; else if (c == 2) once--; if (i >= k) { // 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 string old = songs[i - k]; int d = counts[old] - 1; counts[old] = d; if (d == 1) once++; else if (d == 0) once--; } if (i >= k - 1) outList.Add(once); } Console.WriteLine(string.Join(" ", outList)); }}
C++main.cpp · 29 行
#include <iostream>#include <string>#include <unordered_map>#include <vector>int main() { std::ios::sync_with_stdio(false); int n, k; std::cin >> n >> k; std::vector<std::string> songs(n); for (auto& s : songs) std::cin >> s; // counts[없는 열쇠]는 0으로 새로 생긴다 std::unordered_map<std::string, int> counts; int once = 0; // 지금 구간에서 딱 한 번 나온 곡의 수 for (int i = 0; i < n; i++) { // 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 int c = ++counts[songs[i]]; if (c == 1) once++; else if (c == 2) once--; if (i >= k) { // 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 int d = --counts[songs[i - k]]; if (d == 1) once++; else if (d == 0) once--; } if (i >= k - 1) std::cout << (i >= k ? " " : "") << once; } std::cout << "\n";}
Cmain.c · 53 行
#include <stdio.h>#include <stdlib.h>#include <string.h>/* C에는 사전이 없다. 이름을 정렬해 겹친 것을 지우고, 이분 탐색으로 찾은 자리를 곡 번호로 쓴다 */#define MAXN 9000#define LEN 13static char songs[MAXN][LEN];static char names[MAXN][LEN];static int id[MAXN];static int counts[MAXN];static int cmp(const void* a, const void* b) { return strcmp((const char*)a, (const char*)b);}int main(void) { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%12s", songs[i]); strcpy(names[i], songs[i]); } qsort(names, n, LEN, cmp); int kinds = 0; for (int i = 0; i < n; i++) { if (kinds == 0 || strcmp(names[kinds - 1], names[i]) != 0) { if (kinds != i) strcpy(names[kinds], names[i]); kinds++; } } for (int i = 0; i < n; i++) { char* p = bsearch(songs[i], names, kinds, LEN, cmp); id[i] = (int)((p - names[0]) / LEN); } int once = 0; /* 지금 구간에서 딱 한 번 나온 곡의 수 */ for (int i = 0; i < n; i++) { /* 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 */ int c = ++counts[id[i]]; if (c == 1) once++; else if (c == 2) once--; if (i >= k) { /* 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 */ int d = --counts[id[i - k]]; if (d == 1) once++; else if (d == 0) once--; } if (i >= k - 1) printf(i >= k ? " %d" : "%d", once); } printf("\n"); return 0;}
Craykonmain.cray · 45 行
첫줄 = コンソール.行読み().空白除去().分ける(" ");n = 첫줄[0].数に();k = 첫줄[1].数に();songs = コンソール.行読み().空白除去().分ける(" ");// 사전에 없는 열쇠를 읽으면 멈춘다. 있나로 먼저 확인한다counts = 辞書();once = 0; // 지금 구간에서 딱 한 번 나온 곡의 수out = [];繰り返す (i = 0; i < n; i = i + 1){ song = songs[i]; もし (!counts.含む(song)) { counts[song] = 0; } // 들어오는 곡: 0→1이면 하나 늘고, 1→2이면 하나 준다 counts[song] = counts[song] + 1; もし (counts[song] == 1) { once = once + 1; } でなければ もし (counts[song] == 2) { once = once - 1; } もし (i >= k) { // 나가는 곡: 2→1이면 다시 한 번만 나온 곡이 되고, 1→0이면 빠진다 old = songs[i - k]; counts[old] = counts[old] - 1; もし (counts[old] == 1) { once = once + 1; } でなければ もし (counts[old] == 0) { once = once - 1; } } もし (i >= k - 1) { out.追加(once); }}コンソール.書く(out.つなぐ(" "));