結果
| 問題 | No.204 ゴールデン・ウィーク(2) |
| コンテスト | |
| ユーザー |
gogotea
|
| 提出日時 | 2015-05-09 01:04:26 |
| 言語 | Go1.4 (1.4.2) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 1,000 ms |
| コード長 | 1,767 bytes |
| 記録 | |
| コンパイル時間 | 378 ms |
| コンパイル使用メモリ | 39,652 KB |
| 最終ジャッジ日時 | 2025-12-03 10:19:55 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
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)
src := []byte(pad + s + pad)
buf := make([]byte, len(src))
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)
copy(buf, src)
copy(buf[start:], repl[:])
days := getMaxConsecutiveHolidays(buf)
if days > max {
max = days
}
copy(buf, src)
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)
}
gogotea