import syslines = sys.stdin.read().split("\n")s = lines[0].strip()q = int(lines[1])out = []for line in lines[2:2 + q]: # 빈 칸은 None: s[None:None:-1]처럼 기본값을 Python이 정한다(0을 넣으면 안 된다) parts = [int(p) if p else None for p in line.strip().split(":")] out.append(s[slice(*parts)] or "-")print("\n".join(out))
JavaScriptmain.js · 29 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const s = lines[0].trim();const n = s.length;const q = Number(lines[1]);// 음수 자리는 n을 더하고, 그래도 밖이면 [lo, hi] 안으로 당긴다function fit(x, lo, hi) { if (x < 0) x += n; return Math.min(Math.max(x, lo), hi);}function pySlice(text) { const [a, b, c] = text.trim().split(":"); // c가 없으면 undefined const step = c ? Number(c) : 1; let out = ""; if (step > 0) { const start = a ? fit(Number(a), 0, n) : 0; const stop = b ? fit(Number(b), 0, n) : n; for (let i = start; i < stop; i += step) out += s[i]; } else { // 거꾸로 갈 때 빈 끝은 0번 글자까지 넣는다는 뜻이라 -1로 둔다 const start = a ? fit(Number(a), -1, n - 1) : n - 1; const stop = b ? fit(Number(b), -1, n - 1) : -1; for (let i = start; i > stop; i += step) out += s[i]; } return out || "-";}console.log(lines.slice(2, 2 + q).map(pySlice).join("\n"));
TypeScriptmain.ts · 31 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const s = lines[0].trim();const n = s.length;const q = Number(lines[1]);// 음수 자리는 n을 더하고, 그래도 밖이면 [lo, hi] 안으로 당긴다function fit(x: number, lo: number, hi: number): number { if (x < 0) x += n; return Math.min(Math.max(x, lo), hi);}function pySlice(text: string): string { const [a, b, c]: (string | undefined)[] = text.trim().split(":"); // c가 없으면 undefined const step = c ? Number(c) : 1; let out = ""; if (step > 0) { const start = a ? fit(Number(a), 0, n) : 0; const stop = b ? fit(Number(b), 0, n) : n; for (let i = start; i < stop; i += step) out += s[i]; } else { // 거꾸로 갈 때 빈 끝은 0번 글자까지 넣는다는 뜻이라 -1로 둔다 const start = a ? fit(Number(a), -1, n - 1) : n - 1; const stop = b ? fit(Number(b), -1, n - 1) : -1; for (let i = start; i > stop; i += step) out += s[i]; } return out || "-";}console.log(lines.slice(2, 2 + q).map(pySlice).join("\n"));
JavaMain.java · 42 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { static String s; static int n; // 음수 자리는 n을 더하고, 그래도 밖이면 [lo, hi] 안으로 당긴다 static int fit(int x, int lo, int hi) { if (x < 0) x += n; return Math.min(Math.max(x, lo), hi); } static String pySlice(String text) { String[] p = text.split(":", -1); // -1: 끝의 빈 칸도 버리지 않는다 boolean hasA = !p[0].isEmpty(), hasB = !p[1].isEmpty(); int step = p.length == 3 && !p[2].isEmpty() ? Integer.parseInt(p[2]) : 1; StringBuilder out = new StringBuilder(); if (step > 0) { int start = hasA ? fit(Integer.parseInt(p[0]), 0, n) : 0; int stop = hasB ? fit(Integer.parseInt(p[1]), 0, n) : n; for (int i = start; i < stop; i += step) out.append(s.charAt(i)); } else { // 거꾸로 갈 때 빈 끝은 0번 글자까지 넣는다는 뜻이라 -1로 둔다 int start = hasA ? fit(Integer.parseInt(p[0]), -1, n - 1) : n - 1; int stop = hasB ? fit(Integer.parseInt(p[1]), -1, n - 1) : -1; for (int i = start; i > stop; i += step) out.append(s.charAt(i)); } return out.length() > 0 ? out.toString() : "-"; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); s = in.readLine().trim(); n = s.length(); int q = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int t = 0; t < q; t++) out.append(pySlice(in.readLine().trim())).append('\n'); System.out.print(out); }}
C#Main.cs · 47 行
using System;using System.Text;class Program{ static string s = ""; static int n; // 음수 자리는 n을 더하고, 그래도 밖이면 [lo, hi] 안으로 당긴다 static int Fit(int x, int lo, int hi) { if (x < 0) x += n; return Math.Clamp(x, lo, hi); } static string PySlice(string text) { string[] p = text.Split(':'); // C#의 Split은 빈 칸도 남긴다 bool hasA = p[0] != "", hasB = p[1] != ""; int step = p.Length == 3 && p[2] != "" ? int.Parse(p[2]) : 1; var sb = new StringBuilder(); if (step > 0) { int start = hasA ? Fit(int.Parse(p[0]), 0, n) : 0; int stop = hasB ? Fit(int.Parse(p[1]), 0, n) : n; for (int i = start; i < stop; i += step) sb.Append(s[i]); } else { // 거꾸로 갈 때 빈 끝은 0번 글자까지 넣는다는 뜻이라 -1로 둔다 int start = hasA ? Fit(int.Parse(p[0]), -1, n - 1) : n - 1; int stop = hasB ? Fit(int.Parse(p[1]), -1, n - 1) : -1; for (int i = start; i > stop; i += step) sb.Append(s[i]); } return sb.Length > 0 ? sb.ToString() : "-"; } static void Main() { s = Console.ReadLine().Trim(); n = s.Length; int q = int.Parse(Console.ReadLine().Trim()); var output = new StringBuilder(); for (int t = 0; t < q; t++) output.Append(PySlice(Console.ReadLine().Trim())).Append('\n'); Console.Write(output); }}
C++main.cpp · 53 行
#include <algorithm>#include <iostream>#include <optional>#include <string>#include <vector>std::string s;int n;// 음수 자리는 n을 더하고, 그래도 밖이면 [lo, hi] 안으로 당긴다int fit(int x, int lo, int hi) { if (x < 0) x += n; return std::clamp(x, lo, hi);}std::string pySlice(const std::string &text) { std::vector<std::optional<int>> p; // 빈 칸은 값이 없는 optional std::size_t from = 0; while (true) { std::size_t colon = text.find(':', from); std::string part = text.substr(from, colon == std::string::npos ? std::string::npos : colon - from); p.push_back(part.empty() ? std::nullopt : std::optional<int>(std::stoi(part))); if (colon == std::string::npos) break; from = colon + 1; } int step = p.size() == 3 && p[2] ? *p[2] : 1; std::string out; if (step > 0) { int start = p[0] ? fit(*p[0], 0, n) : 0; int stop = p[1] ? fit(*p[1], 0, n) : n; for (int i = start; i < stop; i += step) out += s[i]; } else { // 거꾸로 갈 때 빈 끝은 0번 글자까지 넣는다는 뜻이라 -1로 둔다 int start = p[0] ? fit(*p[0], -1, n - 1) : n - 1; int stop = p[1] ? fit(*p[1], -1, n - 1) : -1; for (int i = start; i > stop; i += step) out += s[i]; } return out.empty() ? "-" : out;}int main() { int q; std::cin >> s >> q; n = static_cast<int>(s.size()); std::string out; for (int t = 0; t < q; t++) { std::string line; std::cin >> line; out += pySlice(line) + '\n'; } std::cout << out; return 0;}
Cmain.c · 47 行
#include <stdio.h>#include <stdlib.h>#include <string.h>static char s[256];static int n;/* 음수 자리는 n을 더하고, 그래도 밖이면 [lo, hi] 안으로 당긴다 */static int fit(int x, int lo, int hi) { if (x < 0) x += n; return x < lo ? lo : x > hi ? hi : x;}int main(void) { char line[64]; int q; scanf("%255s %d", s, &q); n = (int)strlen(s); for (int t = 0; t < q; t++) { scanf("%63s", line); /* ':'로 칸을 나눈다. strtok는 빈 칸을 건너뛰어서 직접 나눈다 */ char *part[3] = {line, NULL, NULL}; int k = 1; for (char *c = line; *c; c++) if (*c == ':') { *c = '\0'; part[k++] = c + 1; } int hasA = part[0][0] != '\0', hasB = part[1][0] != '\0'; int step = k == 3 && part[2][0] != '\0' ? atoi(part[2]) : 1; char out[256]; int len = 0; if (step > 0) { int start = hasA ? fit(atoi(part[0]), 0, n) : 0; int stop = hasB ? fit(atoi(part[1]), 0, n) : n; for (int i = start; i < stop; i += step) out[len++] = s[i]; } else { /* 거꾸로 갈 때 빈 끝은 0번 글자까지 넣는다는 뜻이라 -1로 둔다 */ int start = hasA ? fit(atoi(part[0]), -1, n - 1) : n - 1; int stop = hasB ? fit(atoi(part[1]), -1, n - 1) : -1; for (int i = start; i > stop; i += step) out[len++] = s[i]; } out[len] = '\0'; puts(len > 0 ? out : "-"); } return 0;}
Craykonmain.cray · 64 行
s = 控制台.读行().去空白();n = s.长度;q = 控制台.读行().转数字();// 음수 자리는 n을 더하고, 그래도 밖이면 [lo, hi] 안으로 당긴다맞추기(x, lo, hi){ 如果 (x < 0) { x = x + n; } 返回 数学.较小值(数学.较大值(x, lo), hi);}循环 (t = 0; t < q; t++){ p = 控制台.读行().去空白().分割(":"); 걸음 = 1; 如果 (p.数量 == 3 && p[2] != "") { 걸음 = p[2].转数字(); } 답 = ""; 如果 (걸음 > 0) { 시작 = 0; 끝 = n; 如果 (p[0] != "") { 시작 = 맞추기(p[0].转数字(), 0, n); } 如果 (p[1] != "") { 끝 = 맞추기(p[1].转数字(), 0, n); } 循环 (k = 시작; k < 끝; k = k + 걸음) { 답 = 답 + s[k]; } } 否则 { // 거꾸로 갈 때 빈 끝은 0번 글자까지 넣는다는 뜻이라 -1로 둔다 시작 = n - 1; 끝 = -1; 如果 (p[0] != "") { 시작 = 맞추기(p[0].转数字(), -1, n - 1); } 如果 (p[1] != "") { 끝 = 맞추기(p[1].转数字(), -1, n - 1); } 循环 (k = 시작; k > 끝; k = k + 걸음) { 답 = 답 + s[k]; } } 如果 (답 == "") { 답 = "-"; } 控制台.输出(답);}