結果
問題 | No.517 壊れたアクセサリー |
ユーザー | ccppjsrb |
提出日時 | 2020-09-16 17:55:10 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,684 bytes |
コンパイル時間 | 11,426 ms |
コンパイル使用メモリ | 231,692 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-22 03:24:54 |
合計ジャッジ時間 | 12,382 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,944 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 1 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,944 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,944 KB |
testcase_14 | AC | 1 ms
6,944 KB |
testcase_15 | AC | 1 ms
6,940 KB |
testcase_16 | AC | 1 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 2 ms
6,940 KB |
ソースコード
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 }