結果

問題 No.204 ゴールデン・ウィーク(2)
ユーザー gogotea
提出日時 2015-05-08 23:52:03
言語 Go1.4
(1.4.2)
結果
WA  
実行時間 -
コード長 1,611 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 40,488 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-25 00:16:54
合計ジャッジ時間 1,788 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18 WA * 28
権限があれば一括ダウンロードができます

ソースコード

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 {
	src := []byte(s)

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

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

func getMaxConsecutiveHolidays(s []byte) int {
	results := bytes.Split(s, []byte("x"))
	max := 0
	for _, v := range results {
		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