結果

問題 No.204 ゴールデン・ウィーク(2)
ユーザー gogotea
提出日時 2015-05-09 00:28:17
言語 Go1.4
(1.4.2)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,760 bytes
コンパイル時間 3,207 ms
コンパイル使用メモリ 38,656 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-24 11:41:56
合計ジャッジ時間 4,694 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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

	fmt.Println(solve(d, line))
}

func solve(d int, s string) int {
	pad := strings.Repeat("x", d)
	pstr := pad + s + pad
	src := []byte(pstr)

	if d <= 0 {
		return getMaxConsecutiveHolidays(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)
		buf := []byte(pstr)
		copy(buf[start:], repl[:])
		days := getMaxConsecutiveHolidays(buf)
		if days > max {
			max = days
		}

		buf = []byte(pstr)
		copy(buf[end-len:], repl[:])
		days = getMaxConsecutiveHolidays(buf)
		if days > max {
			max = days
		}
	}
	return max
}

func getMaxConsecutiveHolidays(s []byte) int {
	res := bytes.Split(s, []byte("x"))
	max := 0
	for _, v := range res {
		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) NextInt() (int, error) {
	token, err := s.Next()
	if err != nil {
		return 0, err
	}
	return strconv.Atoi(token)
}
0