import syslines = sys.stdin.read().split("\n")w, n = map(int, lines[0].split())out = []count = 0for name in lines[1:n + 1]: if len(name) <= w: # 길이가 w 이하면 그대로 out.append(name) continue dot = name.rfind(".") # 마지막 '.', 없으면 -1 ext = name[dot:] if dot >= 0 else "" r = w - 3 - len(ext) # 확장자 앞부분에서 남길 글자 수 if r < 1: # 자리가 없으면 확장자를 따로 두지 않는다 ext = "" r = w - 3 stem = name[:len(name) - len(ext)] head = (r + 1) // 2 # 앞 조각은 올림 tail = r // 2 # 뒤 조각은 내림 # stem[-tail:]은 tail이 0일 때 전체가 되므로 앞에서 센 위치로 자른다 out.append(stem[:head] + "..." + stem[len(stem) - tail:] + ext) count += 1out.append(str(count))print("\n".join(out))
JavaScriptmain.js · 26 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [w, n] = lines[0].split(" ").map(Number);const out = [];let count = 0;for (let i = 1; i <= n; i++) { const name = lines[i]; if (name.length <= w) { // 길이가 w 이하면 그대로 out.push(name); continue; } const dot = name.lastIndexOf("."); // 마지막 '.', 없으면 -1 let ext = dot >= 0 ? name.slice(dot) : ""; let r = w - 3 - ext.length; // 확장자 앞부분에서 남길 글자 수 if (r < 1) { // 자리가 없으면 확장자를 따로 두지 않는다 ext = ""; r = w - 3; } const stem = name.slice(0, name.length - ext.length); const head = Math.ceil(r / 2); // 앞 조각은 올림 const tail = Math.floor(r / 2); // 뒤 조각은 내림 // slice(-tail)은 tail이 0일 때 전체가 되므로 앞에서 센 위치로 자른다 out.push(stem.slice(0, head) + "..." + stem.slice(stem.length - tail) + ext); count++;}out.push(String(count));console.log(out.join("\n"));
TypeScriptmain.ts · 28 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [w, n] = lines[0].split(" ").map(Number);const out: string[] = [];let count = 0;for (let i = 1; i <= n; i++) { const name = lines[i]; if (name.length <= w) { // 길이가 w 이하면 그대로 out.push(name); continue; } const dot = name.lastIndexOf("."); // 마지막 '.', 없으면 -1 let ext = dot >= 0 ? name.slice(dot) : ""; let r = w - 3 - ext.length; // 확장자 앞부분에서 남길 글자 수 if (r < 1) { // 자리가 없으면 확장자를 따로 두지 않는다 ext = ""; r = w - 3; } const stem = name.slice(0, name.length - ext.length); const head = Math.ceil(r / 2); // 앞 조각은 올림 const tail = Math.floor(r / 2); // 뒤 조각은 내림 // slice(-tail)은 tail이 0일 때 전체가 되므로 앞에서 센 위치로 자른다 out.push(stem.slice(0, head) + "..." + stem.slice(stem.length - tail) + ext); count++;}out.push(String(count));console.log(out.join("\n"));
JavaMain.java · 37 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); int w = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(st.nextToken()); StringBuilder out = new StringBuilder(); int count = 0; for (int i = 0; i < n; i++) { String name = in.readLine(); if (name.length() <= w) { // 길이가 w 이하면 그대로 out.append(name).append('\n'); continue; } int dot = name.lastIndexOf('.'); // 마지막 '.', 없으면 -1 String ext = dot >= 0 ? name.substring(dot) : ""; int r = w - 3 - ext.length(); // 확장자 앞부분에서 남길 글자 수 if (r < 1) { // 자리가 없으면 확장자를 따로 두지 않는다 ext = ""; r = w - 3; } String stem = name.substring(0, name.length() - ext.length()); int head = (r + 1) / 2; // 앞 조각은 올림 int tail = r / 2; // 뒤 조각은 내림 // substring(시작, 끝)과 substring(시작) out.append(stem.substring(0, head)).append("...") .append(stem.substring(stem.length() - tail)).append(ext).append('\n'); count++; } out.append(count).append('\n'); System.out.print(out); }}
C#Main.cs · 40 行
using System;using System.Text;class Program{ static void Main() { string[] first = Console.ReadLine()!.Split(' '); int w = int.Parse(first[0]); int n = int.Parse(first[1]); var sb = new StringBuilder(); int count = 0; for (int i = 0; i < n; i++) { string name = Console.ReadLine()!; if (name.Length <= w) // 길이가 w 이하면 그대로 { sb.Append(name).Append('\n'); continue; } int dot = name.LastIndexOf('.'); // 마지막 '.', 없으면 -1 string ext = dot >= 0 ? name.Substring(dot) : ""; int r = w - 3 - ext.Length; // 확장자 앞부분에서 남길 글자 수 if (r < 1) // 자리가 없으면 확장자를 따로 두지 않는다 { ext = ""; r = w - 3; } string stem = name.Substring(0, name.Length - ext.Length); int head = (r + 1) / 2; // 앞 조각은 올림 int tail = r / 2; // 뒤 조각은 내림 // Substring(시작, 길이)와 Substring(시작) sb.Append(stem.Substring(0, head)).Append("...") .Append(stem.Substring(stem.Length - tail)).Append(ext).Append('\n'); count++; } sb.Append(count).Append('\n'); Console.Write(sb); }}
C++main.cpp · 38 行
#include <iostream>#include <sstream>#include <string>int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::string line; std::getline(std::cin, line); // 첫 줄도 getline으로 읽어 줄 바꿈을 남기지 않는다 std::istringstream first(line); int w, n; first >> w >> n; int count = 0; for (int i = 0; i < n; i++) { std::string name; std::getline(std::cin, name); int len = (int)name.size(); if (len <= w) { // 길이가 w 이하면 그대로 std::cout << name << '\n'; continue; } std::size_t dot = name.rfind('.'); // 마지막 '.', 없으면 npos std::string ext = dot != std::string::npos ? name.substr(dot) : ""; int r = w - 3 - (int)ext.size(); // 확장자 앞부분에서 남길 글자 수 if (r < 1) { // 자리가 없으면 확장자를 따로 두지 않는다 ext = ""; r = w - 3; } std::string stem = name.substr(0, name.size() - ext.size()); int head = (r + 1) / 2; // 앞 조각은 올림 int tail = r / 2; // 뒤 조각은 내림 // substr(시작, 길이)와 substr(시작) std::cout << stem.substr(0, head) << "..." << stem.substr(stem.size() - tail) << ext << '\n'; count++; } std::cout << count << '\n'; return 0;}
Cmain.c · 32 行
#include <stdio.h>#include <string.h>int main(void) { static char name[256]; int w, n, count = 0; if (!fgets(name, sizeof name, stdin) || sscanf(name, "%d %d", &w, &n) != 2) return 0; for (int i = 0; i < n; i++) { if (!fgets(name, sizeof name, stdin)) break; int len = (int)strcspn(name, "\r\n"); name[len] = '\0'; if (len <= w) { /* 길이가 w 이하면 그대로 */ puts(name); continue; } const char *dot = strrchr(name, '.'); /* 마지막 '.', 없으면 NULL */ int ext = dot ? (int)(name + len - dot) : 0; int r = w - 3 - ext; /* 확장자 앞부분에서 남길 글자 수 */ if (r < 1) { /* 자리가 없으면 확장자를 따로 두지 않는다 */ ext = 0; r = w - 3; } int stem = len - ext; int head = (r + 1) / 2; /* 앞 조각은 올림 */ int tail = r / 2; /* 뒤 조각은 내림 */ /* %.*s는 길이만큼만 찍는다. 뒤 조각과 확장자는 이어져 있으니 끝까지 찍는다 */ printf("%.*s...%s\n", head, name, name + stem - tail); count++; } printf("%d\n", count); return 0;}
Craykonmain.cray · 36 行
first = コンソール.数読み();w = first[0];n = first[1];count = 0;繰り返す (i = 0; i < n; i++){ name = コンソール.行読み(); もし (name.長さ <= w) { コンソール.書く(name); 次へ; } dot = name.長さ - 1; // 끝에서부터 마지막 '.'을 찾는다. 없으면 -1 間 (dot >= 0 && name[dot] != ".") { dot -= 1; } ext = ""; もし (dot >= 0) { ext = name.切り出す(dot); } r = w - 3 - ext.長さ; // 확장자 앞부분에서 남길 글자 수 もし (r < 1) { ext = ""; r = w - 3; } stem = name.切り出す(0, name.長さ - ext.長さ); tail = 数学.商(r, 2); // 뒤 조각은 내림 head = r - tail; // 앞 조각은 올림 // 부분문자열(시작, 길이)와 부분문자열(시작) コンソール.書く(stem.切り出す(0, head) + "..." + stem.切り出す(stem.長さ - tail) + ext); count += 1;}コンソール.書く(count);