結果

問題 No.204 ゴールデン・ウィーク(2)
ユーザー gogoteagogotea
提出日時 2015-05-09 20:08:21
言語 Go1.4
(1.4.2)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 3,297 bytes
コンパイル時間 2,950 ms
コンパイル使用メモリ 39,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-25 23:41:01
合計ジャッジ時間 2,189 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"regexp"
	"strconv"
)

func main() {
	sc := NewScanner(os.Stdin)
	d, _ := sc.NextInt()
	line1, _ := sc.NextLineBytes()
	line2, _ := sc.NextLineBytes()

	fmt.Println(solve(d, append(line1, line2...)))
}

func solve(d int, s []byte) int {
	pad := bytes.Repeat([]byte{'x'}, d)
	src := append(pad, s...)
	src = append(src, pad...)
	buf := make([]byte, len(src))

	if d <= 0 {
		return getMaxHolidays(src)
	}

	re := regexp.MustCompile(`x+`)
	indexes := re.FindAllIndex(src, -1)
	max := 0
	for _, v := range indexes {
		start, end := v[0], v[1]
		len := end - start
		if len > d {
			len = d
		}
		repl := bytes.Repeat([]byte{'o'}, len)
		copy(buf, src)
		copy(buf[start:], repl[:])
		days := getMaxHolidays(buf)
		if days > max {
			max = days
		}

		if start != end-len {
			copy(buf, src)
			copy(buf[end-len:], repl[:])
			days = getMaxHolidays(buf)
			if days > max {
				max = days
			}
		}
	}
	return max
}

// BenchmarkGetMaxHolidays			30000000     44.5 ns/op    0 B/op    0 allocs/op
// BenchmarkGetMaxHolidays2			 1000000   1027 ns/op    176 B/op    1 allocs/op
// BenchmarkGetMaxHolidays3				100000	15208 ns/op		1968 B/op		38 allocs/op
// BenchmarkGetMaxHolidaysMethod  200000   6085 ns/op    352 B/op    8 allocs/op
func getMaxHolidays(s []byte) int {
	var cnt, max int
	for _, b := range s {
		switch {
		case b == byte('o'):
			cnt++
		default:
			cnt = 0
		}
		if cnt > max {
			max = cnt
		}
	}
	return max
}

func getMaxHolidays2(s []byte) int {
	max := 0
	for _, v := range bytes.Split(s, []byte{'x'}) {
		if len(v) > max {
			max = len(v)
		}
	}
	return max
}

func getMaxHolidays3(s []byte) int {
	re := regexp.MustCompile(`o+`)
	max := 0
	for _, v := range re.FindAll(s, -1) {
		if len(v) > max {
			max = len(v)
		}
	}
	return max
}

type holidays struct {
	re *regexp.Regexp
}

func newHolidays() *holidays {
	return &holidays{
		re: regexp.MustCompile(`o+`),
	}
}

func (h *holidays) getMaxHolidays(s []byte) int {
	max := 0
	for _, v := range h.re.FindAll(s, -1) {
		if len(v) > max {
			max = len(v)
		}
	}
	return max
}

type scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader) *scanner {
	return &scanner{
		bufio.NewScanner(r),
	}
}

func (s *scanner) Next() (string, error) {
	s.Scanner.Split(bufio.ScanWords)
	return s.nextToken()
}

func (s *scanner) NextLine() (string, error) {
	s.Scanner.Split(bufio.ScanLines)
	return s.nextToken()
}

func (s *scanner) nextToken() (string, error) {
	sc := s.Scanner
	if sc.Scan() {
		return sc.Text(), nil
	}
	if sc.Err() != nil {
		return "", sc.Err()
	}
	return "", io.EOF
}

func (s *scanner) NextBytes() ([]byte, error) {
	s.Scanner.Split(bufio.ScanWords)
	return s.nextBytes()
}

func (s *scanner) NextLineBytes() ([]byte, error) {
	s.Scanner.Split(bufio.ScanLines)
	return s.nextBytes()
}

func (s *scanner) nextBytes() ([]byte, error) {
	sc := s.Scanner
	if sc.Scan() {
		return sc.Bytes(), nil
	}
	if sc.Err() != nil {
		return nil, sc.Err()
	}
	return nil, io.EOF
}

func (s *scanner) NextInt() (int, error) {
	token, err := s.Next()
	if err != nil {
		return 0, err
	}
	return strconv.Atoi(token)
}

func (s *scanner) NextLong() (int64, error) {
	token, err := s.Next()
	if err != nil {
		return 0, err
	}
	return strconv.ParseInt(token, 10, 64)
}
0