import syssections = 0masked = 0out = []line = sys.stdin.readline().rstrip("\n")# END 줄이 끝 표시다. 그 뒤의 줄은 읽지 않는다while line != "END": pos = 0 while True: i = line.find("[[", pos) if i < 0: break j = line.find("]]", i + 2) # 여는 [[ 바로 뒤부터 찾는다 if j < 0: break # 닫히지 않은 [[ 뒤는 그대로 둔다 n = j - (i + 2) line = line[: i + 2] + "*" * n + line[j:] sections += 1 masked += n pos = j + 2 out.append(line) line = sys.stdin.readline().rstrip("\n")out.append(f"{sections} {masked}")print("\n".join(out))
JavaScriptmain.js · 23 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");let sections = 0;let masked = 0;const out = [];// END 줄이 끝 표시다. 그 뒤의 줄은 보지 않는다for (let k = 0; lines[k] !== "END"; k++) { let line = lines[k]; let pos = 0; while (true) { const i = line.indexOf("[[", pos); if (i < 0) break; const j = line.indexOf("]]", i + 2); // 여는 [[ 바로 뒤부터 찾는다 if (j < 0) break; // 닫히지 않은 [[ 뒤는 그대로 둔다 const n = j - (i + 2); line = line.slice(0, i + 2) + "*".repeat(n) + line.slice(j); sections++; masked += n; pos = j + 2; } out.push(line);}out.push(`${sections} ${masked}`);console.log(out.join("\n"));
TypeScriptmain.ts · 24 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");let sections = 0;let masked = 0;const out: string[] = [];// END 줄이 끝 표시다. 그 뒤의 줄은 보지 않는다for (let k = 0; lines[k] !== "END"; k++) { let line: string = lines[k]; let pos = 0; while (true) { const i: number = line.indexOf("[[", pos); if (i < 0) break; const j: number = line.indexOf("]]", i + 2); // 여는 [[ 바로 뒤부터 찾는다 if (j < 0) break; // 닫히지 않은 [[ 뒤는 그대로 둔다 const n: number = j - (i + 2); line = line.slice(0, i + 2) + "*".repeat(n) + line.slice(j); sections++; masked += n; pos = j + 2; } out.push(line);}out.push(`${sections} ${masked}`);console.log(out.join("\n"));
JavaMain.java · 31 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int sections = 0, masked = 0; StringBuilder out = new StringBuilder(); String line = br.readLine(); // END 줄이 끝 표시다. 문자열 비교는 equals로 한다 while (!line.equals("END")) { int pos = 0; while (true) { int i = line.indexOf("[[", pos); if (i < 0) break; int j = line.indexOf("]]", i + 2); // 여는 [[ 바로 뒤부터 찾는다 if (j < 0) break; // 닫히지 않은 [[ 뒤는 그대로 둔다 int n = j - (i + 2); line = line.substring(0, i + 2) + "*".repeat(n) + line.substring(j); sections++; masked += n; pos = j + 2; } out.append(line).append('\n'); line = br.readLine(); } out.append(sections).append(' ').append(masked).append('\n'); System.out.print(out); }}
C#Main.cs · 34 行
using System;using System.Text;class Program{ static void Main() { int sections = 0, masked = 0; var sb = new StringBuilder(); string line = Console.ReadLine(); // END 줄이 끝 표시다. 그 뒤의 줄은 읽지 않는다 while (line != "END") { int pos = 0; while (true) { // string으로 찾으면 문화권 비교가 되므로 Ordinal을 준다 int i = line.IndexOf("[[", pos, StringComparison.Ordinal); if (i < 0) break; int j = line.IndexOf("]]", i + 2, StringComparison.Ordinal); // 여는 [[ 바로 뒤부터 if (j < 0) break; // 닫히지 않은 [[ 뒤는 그대로 둔다 int n = j - (i + 2); line = line.Substring(0, i + 2) + new string('*', n) + line.Substring(j); sections++; masked += n; pos = j + 2; } sb.Append(line).Append('\n'); line = Console.ReadLine(); } sb.Append(sections).Append(' ').Append(masked).Append('\n'); Console.Write(sb); }}
C++main.cpp · 24 行
#include <iostream>#include <string>int main() { std::string line; int sections = 0, masked = 0; // END 줄이 끝 표시다. 그 뒤의 줄은 읽지 않는다 while (std::getline(std::cin, line) && line != "END") { std::size_t pos = 0; while (true) { std::size_t i = line.find("[[", pos); if (i == std::string::npos) break; std::size_t j = line.find("]]", i + 2); // 여는 [[ 바로 뒤부터 찾는다 if (j == std::string::npos) break; // 닫히지 않은 [[ 뒤는 그대로 둔다 std::size_t n = j - (i + 2); line.replace(i + 2, n, std::string(n, '*')); sections++; masked += static_cast<int>(n); pos = j + 2; } std::cout << line << '\n'; } std::cout << sections << ' ' << masked << '\n';}
Cmain.c · 27 行
#include <stdio.h>#include <string.h>int main(void) { char line[256]; int sections = 0, masked = 0; /* END 줄이 끝 표시다. 그 뒤의 줄은 읽지 않는다 */ while (fgets(line, sizeof line, stdin)) { line[strcspn(line, "\r\n")] = '\0'; if (strcmp(line, "END") == 0) break; char *pos = line; while (1) { char *open = strstr(pos, "[["); if (open == NULL) break; char *close = strstr(open + 2, "]]"); /* 여는 [[ 바로 뒤부터 찾는다 */ if (close == NULL) break; /* 닫히지 않은 [[ 뒤는 그대로 둔다 */ /* 글자 수가 같으니 제자리에서 바꾼다 */ for (char *p = open + 2; p < close; p++) *p = '*'; sections++; masked += (int)(close - (open + 2)); pos = close + 2; } puts(line); } printf("%d %d\n", sections, masked); return 0;}
Craykonmain.cray · 32 行
구간수 = 0;가린수 = 0;줄 = 控制台.读行();// END 줄이 끝 표시다. 그 뒤의 줄은 읽지 않는다当 (줄 != "END"){ 위치 = 0; 当 (真) { // 자리찾기에는 시작 자리를 줄 수 없어서, 잘라 낸 뒤 찾고 떼어 낸 만큼 더한다 i = 줄.截取(위치).查找位置("[["); 如果 (i < 0) { 跳出; } i += 위치; j = 줄.截取(i + 2).查找位置("]]"); // 여는 [[ 바로 뒤부터 찾는다 如果 (j < 0) { 跳出; // 닫히지 않은 [[ 뒤는 그대로 둔다 } j += i + 2; n = j - (i + 2); 줄 = 줄.截取(0, i + 2) + "*".重复(n) + 줄.截取(j); 구간수 += 1; 가린수 += n; 위치 = j + 2; } 控制台.输出(줄); 줄 = 控制台.读行();}控制台.输出(구간수 + " " + 가린수);