結果

問題 No.205 マージして辞書順最小
ユーザー yukirinyukirin
提出日時 2016-03-31 16:36:57
言語 Go
(1.22.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,224 bytes
コンパイル時間 10,863 ms
コンパイル使用メモリ 239,652 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-19 07:12:13
合計ジャッジ時間 11,756 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,948 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	n, b := nextInt(), make([]byte, 0, 2500)
	q := make(priorityQ, 0, 50)

	heap.Init(&q)
	for i := 0; i < n; i++ {
		heap.Push(&q, nextLine())
	}

	for q.Len() > 0 {
		s := heap.Pop(&q).(string)
		b = append(b, s[0])

		if len(s) == 1 {
			continue
		}

		heap.Push(&q, s[1:])
	}

	fmt.Println(string(b))
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}

func compare(a, b string) bool {
	min, lb := len(a), len(b)
	if lb < min {
		min = lb
	}
	for i := 0; i < min; i++ {
		if a[i] < b[i] {
			return true
		}

		if a[i] > b[i] {
			return false
		}
	}
	if len(a) > len(b) {
		return true
	}
	return false
}

type priorityQ []string

func (h priorityQ) Len() int {
	return len(h)
}

func (h priorityQ) Less(i, j int) bool {
	return compare(h[i], h[j])
}

func (h priorityQ) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *priorityQ) Push(x interface{}) {
	*h = append(*h, x.(string))
}

func (h *priorityQ) Pop() interface{} {
	x := (*h)[len(*h)-1]
	*h = (*h)[0 : len(*h)-1]
	return x
}
0