#1181: 단어 정렬

1181번: 단어 정렬

JavaScript
var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

reader.on('line', (line) => {
  lines.push(line);
});

reader.on('close', () => {
    console.log(Array.from(new Set(lines)).slice(1).sort().sort((x,y)=>x.length-y.length).join("\n"))
});

좋은 주말 되시길 바랍니다 :smiley:

1 Like
C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int N; std::cin >> N;
    std::vector<std::string> v(N);
    
    for (auto& s : v) {
        std::cin >> s;
    }
    
    std::sort(v.begin(), v.end(), [](const auto& l, const auto& r) {
        return l.length() == r.length() ? l < r : l.length() < r.length();
    });
    
    v.erase(std::unique(v.begin(), v.end()), v.end());
    
    for (auto&& s : v) {
        std::cout << s << '\n';
    }
    
    return 0;
}
Go
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	in.Scan()
	n, _ := strconv.Atoi(in.Text())

	words := make([]string, 0, n)
	for i := 0; i < n; i++ {
		in.Scan()
		words = append(words, in.Text())
	}

	sort.Sort(byLength(words))
	for i, word := range words {
		if i > 0 && words[i-1] == word {
			continue
		}
		fmt.Fprintln(out, word)
	}
}

type byLength []string

func (w byLength) Len() int {
	return len(w)
}

func (w byLength) Less(i, j int) bool {
	if len(w[i]) != len(w[j]) {
		return len(w[i]) < len(w[j])
	}
	return w[i] < w[j]
}

func (w byLength) Swap(i, j int) {
	w[i], w[j] = w[j], w[i]
}

백준 계정이 없어서 테스트는 못 해봤지만 한 번 올려봅니다.

Bash
tail -n +2 input.txt | awk '{printf "%02d\t%s\n", length($0), $0}' | sort | uniq | awk '{print $2}'
Python 3
N = int(input())
words = []
for i in range(N):
    words.append(input())

words_set = set(words)
words = list(words_set)
words.sort()
words = sorted(words, key=len)

for i in words:
    print(i)
Java 15
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String[] input = new String[N+1];
        for (int i = 0; i <= N; i++) input[i] = sc.nextLine();
        sc.close();
        
//        for (String s : input) System.out.println(s);
        
        // https://hianna.tistory.com/554
        HashSet<String> set = new HashSet<>(Arrays.asList(input));
        String[] resultArr = set.toArray(new String[0]);
        
        Arrays.sort(resultArr);
        // https://stackoverflow.com/questions/35866240/how-to-sort-string-array-by-length-using-arrays-sort
        Arrays.sort(resultArr, Comparator.comparingInt(String::length));
    
        System.out.printf("%s", resultArr[0]);
        for(int i = 1; i < resultArr.length; i++){
            System.out.println(resultArr[i]);
        }
    }
}

자바로 안 풀리던게 이렇게 간단히 풀리다니… ㄷㄷ

+) 아니 뭐야 자바도 파이썬이랑 똑같이 풀리네

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp(const void *a, const void *b){
	char *A=(char *)a, *B=(char *)b;
	int lena=strlen(A), lenb=strlen(B);
    return (lena==lenb) ? strcmp(A, B) : lena-lenb;
}

int main(void){
	int i, N;
	char str[20000][51], tmp[51]={0,};

	scanf("%d", &N);
	for(i=0; i<N; ++i){
		scanf("%s", str[i]);
	}
	qsort(str, N, sizeof(str[0]), cmp);

	for(i=0; i<N; ++i){
		if(strcmp(tmp, str[i])){
			strcpy(tmp, str[i]);
			printf("%s\n", str[i]);
		}
	}

	return 0;
}