結果

問題 No.204 ゴールデン・ウィーク(2)
ユーザー gogoteagogotea
提出日時 2015-05-09 00:28:17
言語 Go1.4
(1.4.2)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,760 bytes
コンパイル時間 3,065 ms
コンパイル使用メモリ 40,096 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-25 23:38:26
合計ジャッジ時間 4,831 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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