結果

問題 No.672 最長AB列
ユーザー tsuchinaga
提出日時 2019-03-06 09:36:34
言語 Go
(1.23.4)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 557 bytes
コンパイル時間 10,924 ms
コンパイル使用メモリ 227,324 KB
実行使用メモリ 13,196 KB
最終ジャッジ日時 2024-06-23 14:31:08
合計ジャッジ時間 11,809 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	maxSize := 2 * int(math.Pow10(5))
	rdr := bufio.NewReaderSize(os.Stdin, maxSize)
	buf := make([]byte, 0, maxSize)
	for {
		l, p, _ := rdr.ReadLine()
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	s := string(buf)

	n := 0
	m := map[int]int{0: -1}
	max := 0
	for i := 0; i < len(s); i++ {
		if string(s[i]) == "A" {
			n++
		} else {
			n--
		}
		if v, ok := m[n]; !ok {
			m[n] = i
		} else if max < i-v {
			max = i - v
		}
		// fmt.Println(n, i, m, m[n])
	}
	fmt.Println(max)
}
0