結果
問題 | No.517 壊れたアクセサリー |
ユーザー |
![]() |
提出日時 | 2017-05-29 14:14:03 |
言語 | Go (1.23.4) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,710 bytes |
コンパイル時間 | 15,310 ms |
コンパイル使用メモリ | 240,896 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 18:08:53 |
合計ジャッジ時間 | 13,841 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
package mainimport ("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 intvar s, t stringfor 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.Readerbuf []bytep int}func newScanner() *scanner {rdr := bufio.NewReaderSize(os.Stdin, 1000)return &scanner{r: rdr}}func (s *scanner) next() string {s.pre()start := s.pfor ; 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.ps.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}}}