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) }