結果

問題 No.517 壊れたアクセサリー
ユーザー fmhrfmhr
提出日時 2017-05-29 14:14:03
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,710 bytes
コンパイル時間 16,877 ms
コンパイル使用メモリ 220,580 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 16:54:17
合計ジャッジ時間 13,040 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

func main() {
	log.SetFlags(log.Lshortfile)
	// log.Println("hello")
	sc := newScanner()
	N := sc.nextInt()
	A := make([]string, N)
	for i := 0; i < N; i++ {
		A[i] = sc.nextLine()
	}
	M := sc.nextInt()
	B := make([]string, M)
	for i := 0; i < M; i++ {
		B[i] = sc.nextLine()
	}
	var c int
	var s, t string
	for i := 0; i < N; i++ {
		for j := 0; j < M; j++ {
			if A[i][0] == B[j][0] {
				c++
				s += A[i]
				t += B[j]
			}
		}
	}
	if c != 1 {
		fmt.Println("-1")
	} else {
		for len(s) != len(t) {
			// 短い方に足していく
			if len(s) > len(t) {
				d := s[len(t)]
				for j := 0; j < M; j++ {
					if B[j][0] == d {
						t += B[j]
						break
					}
				}
			} else {
				d := t[len(s)]
				for i := 0; i < N; i++ {
					if A[i][0] == d {
						s += A[i]
						break
					}
				}
			}
		}
		fmt.Println(s)
	}
	return
}

type scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func newScanner() *scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &scanner{r: rdr}
}
func (s *scanner) next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *scanner) nextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *scanner) nextInt() int {
	v, _ := strconv.Atoi(s.next())
	return v
}

func (s *scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
0