from collections import defaultdictgroups = defaultdict(list)for w in input().split(): groups[w[0]].append(w)for letter in sorted(groups): print(f"{letter}: {' '.join(groups[letter])}")
JavaScriptmain.js · 8 行
const words = require("fs").readFileSync(0, "utf8").trim().split(/\s+/);const groups = new Map();for (const w of words) { if (!groups.has(w[0])) groups.set(w[0], []); groups.get(w[0]).push(w);}const letters = [...groups.keys()].sort();console.log(letters.map((c) => `${c}: ${groups.get(c).join(" ")}`).join("\n"));
TypeScriptmain.ts · 9 行
import { readFileSync } from "node:fs";const words: string[] = readFileSync(0, "utf8").trim().split(/\s+/);const groups = new Map<string, string[]>();for (const w of words) { if (!groups.has(w[0])) groups.set(w[0], []); groups.get(w[0])!.push(w);}const letters: string[] = [...groups.keys()].sort();console.log(letters.map((c) => `${c}: ${groups.get(c)!.join(" ")}`).join("\n"));
JavaMain.java · 20 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.TreeMap;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Map<Character, List<String>> groups = new TreeMap<>(); for (String w : in.readLine().trim().split(" ")) groups.computeIfAbsent(w.charAt(0), k -> new ArrayList<>()).add(w); StringBuilder out = new StringBuilder(); for (Map.Entry<Character, List<String>> e : groups.entrySet()) out.append(e.getKey()).append(": ").append(String.join(" ", e.getValue())).append('\n'); System.out.print(out); }}
C#Main.cs · 20 行
using System;using System.Collections.Generic;class Program{ static void Main() { var groups = new SortedDictionary<char, List<string>>(); foreach (string w in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)) { if (!groups.TryGetValue(w[0], out var list)) { list = new List<string>(); groups[w[0]] = list; } list.Add(w); } foreach (var kv in groups) Console.WriteLine($"{kv.Key}: {string.Join(" ", kv.Value)}"); }}
C++main.cpp · 15 行
#include <iostream>#include <map>#include <string>#include <vector>int main() { std::map<char, std::vector<std::string>> groups; std::string w; while (std::cin >> w) groups[w[0]].push_back(w); // 없는 열쇠는 빈 vector가 생긴다 for (const auto& [letter, list] : groups) { std::cout << letter << ':'; for (const auto& x : list) std::cout << ' ' << x; std::cout << '\n'; }}
Cmain.c · 26 行
#include <stdio.h>/* C에는 사전이 없다: 첫 글자가 a~z 26가지뿐이니 글자마다 칸이 있는 고정 표를 쓴다 */static char words[1000][13];static int next_in_group[1000];int main(void) { int head[26], tail[26]; for (int c = 0; c < 26; c++) head[c] = tail[c] = -1; int n = 0; while (scanf("%12s", words[n]) == 1) { int c = words[n][0] - 'a'; next_in_group[n] = -1; if (head[c] < 0) head[c] = n; else next_in_group[tail[c]] = n; tail[c] = n; n++; } for (int c = 0; c < 26; c++) { if (head[c] < 0) continue; printf("%c:", 'a' + c); for (int i = head[c]; i >= 0; i = next_in_group[i]) printf(" %s", words[i]); printf("\n"); } return 0;}