A game chat room masks banned words with asterisks.
The first line gives the number of banned words n, and the second line gives the n banned words separated by spaces.
The third line gives one chat line, made of lowercase English words separated by single spaces.
Every word of the chat that is exactly equal to a word in the banned list is replaced by as many asterisks (*) as it has letters.
A word that only contains a banned word as a part is not replaced. For example, even if cat is banned, category stays as it is.
The banned list may contain the same word twice.
Print the masked chat on one line, with one space between words.
Input
1 ≤ n ≤ 100, 1 to 1,000 words in the chat, banned words and words are 1 to 20 lowercase letters
Output
One line: the masked chat
Example 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
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
The trap is mixing up "is it in the list?" with "is it inside the string?". The same operation name has both meanings. Python in looks for an equal element in a list but for a substring in a string. If you leave the banned line as a string without split(), "at" in "cat dog" is true. JS/TS includes is likewise an equal element on arrays and a substring on strings. Craykon Contains has the same difference between lists and strings. Java has List.contains, C# List.Contains (for arrays, LINQ Contains or Array.IndexOf), and C++ std::find(v.begin(), v.end(), w) != v.end(). std::vector has no contains member (C++23 adds the algorithm std::ranges::contains(v, w)). C has no list search, so you search with a loop and strcmp. Comparing with == in C compares addresses, not contents, so nothing matches. Using replace on the whole line also masks the cat inside category, so ask word by word. Making the asterisks: Python "*" * len(w), JS/TS/Java "*".repeat(n), C# new string('*', n), C++ std::string(n, '*'), Craykon "*".Repeat(n), and in C putchar n times.
Pythonmain.py · 10 lines
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 lines
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 lines
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 lines
#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 lines
#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 lines
n = Console.ReadLine().ToNumber();banned = Console.ReadLine().Trim().Split(); // 낱말 목록words = Console.ReadLine().Trim().Split();out = [];foreach (w in words){ // 목록의 있나는 같은 낱말을 찾는다(문자열의 있나는 부분 문자열을 찾는다) if (banned.Contains(w)) { out.Add("*".Repeat(w.Length)); } else { out.Add(w); }}Console.WriteLine(out.Join(" "));