게임 채팅방이 금지어를 별표로 가린다.
첫 줄에 금지어의 개수 n이, 둘째 줄에 금지어 n개가 빈칸으로 띄워 주어진다.
셋째 줄에 채팅 한 줄이 주어진다. 채팅은 영어 소문자 낱말들이 빈칸 하나씩으로 띄워져 있다.
채팅의 낱말 가운데 금지어 목록에 있는 낱말과 통째로 같은 낱말은 그 낱말의 글자 수만큼의 별표(*)로 바꾼다.
금지어가 다른 낱말의 일부로 들어 있으면 그 낱말은 바꾸지 않는다. 예를 들어 금지어가 cat이어도 category는 그대로 둔다.
금지어 목록에는 같은 낱말이 두 번 나올 수도 있다.
바꾼 채팅을 낱말 사이에 빈칸 하나씩 두어 한 줄에 출력하라.
입력
1 ≤ n ≤ 100, 채팅의 낱말 수 1 ~ 1,000, 금지어와 낱말은 영어 소문자 1 ~ 20글자
출력
가린 채팅 한 줄
예시 1 · stdin
2
cat dog
the cat sat on a category of dogs with a dog
stdout
the *** sat on a category of dogs with a ***
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
"목록에 있나"와 "문자열 안에 있나"를 헷갈리는 게 이 문제의 함정이에요. 같은 이름의 연산이 두 가지 뜻을 가져요. Python in은 목록에서는 같은 원소를, 문자열에서는 부분 문자열을 찾아요. 금지어 줄을 split()하지 않고 문자열로 두면 "at" in "cat dog"가 참이 돼요. JS·TS includes도 배열에서는 같은 원소, 문자열에서는 부분 문자열이에요. 크레이콘 있나도 목록과 문자열에서 같은 차이가 있어요. Java는 List.contains, C#은 List.Contains(배열이면 LINQ Contains 또는 Array.IndexOf), C++는 std::find(v.begin(), v.end(), w) != v.end()예요. std::vector에는 contains가 없어요(C++23부터는 std::ranges::contains(v, w)도 있어요). C에는 목록 찾기가 없어서 반복문과 strcmp로 직접 찾아요. C에서 ==로 비교하면 내용이 아니라 주소를 비교해서 하나도 안 걸려요. 문장 전체에 replace를 쓰면 category 속 cat까지 가려지니, 낱말 단위로 물어야 해요. 별표 만들기: Python "*" * len(w), JS·TS·Java "*".repeat(n), C# new string('*', n), C++ std::string(n, '*'), 크레이콘 "*".되풀이(n), C는 putchar를 n번 해요.
Pythonmain.py · 10 줄
n = int(input())banned = input().split() # 문자열이 아니라 낱말 목록으로 둔다words = input().split()out = []for w in words: if w in banned: # 목록에서 통째로 같은 낱말을 찾는다 out.append("*" * len(w)) else: out.append(w)print(" ".join(out))
JavaScriptmain.js · 6 줄
const lines = require("fs").readFileSync(0, "utf8").split("\n");const banned = lines[1].trim().split(/\s+/); // 낱말 배열const words = lines[2].trim().split(/\s+/);// 배열의 includes는 같은 낱말을 찾는다(문자열의 includes는 부분 문자열을 찾는다)const out = words.map((w) => (banned.includes(w) ? "*".repeat(w.length) : w));console.log(out.join(" "));
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Arrays;import java.util.List;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in.readLine(); List<String> banned = Arrays.asList(in.readLine().trim().split(" ")); String[] words = in.readLine().trim().split(" "); StringBuilder out = new StringBuilder(); for (int i = 0; i < words.length; i++) { if (i > 0) out.append(' '); // List.contains는 equals로 비교한다(==는 같은 객체인지를 본다) out.append(banned.contains(words[i]) ? "*".repeat(words[i].length()) : words[i]); } System.out.println(out); }}
C#Main.cs · 21 줄
using System;using System.Collections.Generic;class Program{ static void Main() { Console.ReadLine(); var banned = new List<string>(Console.ReadLine().Trim().Split(' ')); string[] words = Console.ReadLine().Trim().Split(' '); for (int i = 0; i < words.Length; i++) { // List.Contains는 낱말이 통째로 같은지 본다 if (banned.Contains(words[i])) { words[i] = new string('*', words[i].Length); } } Console.WriteLine(string.Join(" ", words)); }}
C++main.cpp · 21 줄
#include <algorithm>#include <iostream>#include <string>#include <vector>int main() { int n; std::cin >> n; std::vector<std::string> banned(n); for (auto& b : banned) std::cin >> b; std::string w; bool first = true; while (std::cin >> w) { if (!first) std::cout << ' '; first = false; // std::find로 같은 낱말을 찾는다(vector에는 contains 멤버가 없다) bool hit = std::find(banned.begin(), banned.end(), w) != banned.end(); std::cout << (hit ? std::string(w.size(), '*') : w); } std::cout << '\n';}
Cmain.c · 31 줄
#include <stdio.h>#include <string.h>static char banned[100][21];static int n;/* 목록에 같은 낱말이 있나: 내용 비교는 strcmp(==는 주소를 비교한다) */int contains(const char *w) { for (int i = 0; i < n; i++) { if (strcmp(banned[i], w) == 0) return 1; } return 0;}int main(void) { char w[21]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%20s", banned[i]); int first = 1; while (scanf("%20s", w) == 1) { if (!first) putchar(' '); first = 0; if (contains(w)) { for (size_t k = 0; k < strlen(w); k++) putchar('*'); } else { fputs(w, stdout); } } putchar('\n'); return 0;}
Craykonmain.cray · 17 줄
n = 콘솔.줄읽기().수로변환();banned = 콘솔.줄읽기().공백제거().나누기(); // 낱말 목록words = 콘솔.줄읽기().공백제거().나누기();out = [];각각 (w 에서 words){ // 목록의 있나는 같은 낱말을 찾는다(문자열의 있나는 부분 문자열을 찾는다) 만약 (banned.있나(w)) { out.넣기("*".되풀이(w.길이)); } 아니면 { out.넣기(w); }}콘솔.쓰기(out.잇기(" "));