結果

問題 No.517 壊れたアクセサリー
ユーザー ccppjsrbccppjsrb
提出日時 2020-09-16 17:55:10
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,684 bytes
コンパイル時間 14,964 ms
コンパイル使用メモリ 214,116 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 03:44:22
合計ジャッジ時間 13,742 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	aa := newAccessary()
	for i := 0; i < n; i++ {
		a := getNextString(scanner)
		for j := 0; j < len(a); j++ {
			aa.used[a[j]-'A'] = true
			if j > 0 {
				aa.prev[a[j]-'A'] = int(a[j-1] - 'A')
			}
			if j+1 < len(a) {
				aa.next[a[j]-'A'] = int(a[j+1] - 'A')
			}
		}
	}
	m := getNextInt(scanner)
	bb := newAccessary()
	for i := 0; i < m; i++ {
		a := getNextString(scanner)
		for j := 0; j < len(a); j++ {
			bb.used[a[j]-'A'] = true
			if j > 0 {
				bb.prev[a[j]-'A'] = int(a[j-1] - 'A')
			}
			if j+1 < len(a) {
				bb.next[a[j]-'A'] = int(a[j+1] - 'A')
			}
		}
	}

	for i := 0; i < 26; i++ {
		if aa.prev[i] != bb.prev[i] {
			if aa.prev[i] == -1 {
				aa.prev[i] = bb.prev[i]
			} else {
				bb.prev[i] = aa.prev[i]
			}
		}
		if aa.next[i] != bb.next[i] {
			if aa.next[i] == -1 {
				aa.next[i] = bb.next[i]
			} else {
				bb.next[i] = aa.next[i]
			}
		}
	}
	head := -1
	for i := 0; i < 26; i++ {
		if aa.used[i] && bb.used[i] && aa.prev[i] == -1 && bb.prev[i] == -1 {
			if head != -1 {
				fmt.Fprintln(writer, -1)
				return
			}
			head = i
		}
	}
	ans := make([]string, 0)
	for head != -1 {
		ans = append(ans, fmt.Sprintf("%c", 'A'+head))
		head = aa.next[head]
	}
	fmt.Fprintln(writer, strings.Join(ans, ""))
}

type accessary struct {
	used       [26]bool
	prev, next [26]int
}

func newAccessary() *accessary {
	a := &accessary{}
	for i := 0; i < 26; i++ {
		a.prev[i] = -1
		a.next[i] = -1
	}
	return a
}
0