結果

問題 No.2283 Prohibit Three Consecutive
ユーザー ynm3nynm3n
提出日時 2023-04-28 23:43:20
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 4,867 bytes
コンパイル時間 14,393 ms
コンパイル使用メモリ 236,264 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-29 00:45:49
合計ジャッジ時間 15,163 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 4 ms
5,376 KB
testcase_12 WA -
testcase_13 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"math/big"
	"os"
	"strconv"
)

// 解答欄
func solve(sc *myScanner, wr *myWriter) {
	tails := [][]rune{
		{'0', '0'},
		{'0', '1'},
		{'1', '0'},
		{'1', '1'},
	}
	flip := func(r rune) rune {
		if r == '0' {
			return '1'
		}
		return '0'
	}
	f := func(n int, orig []rune) bool {
		s := make([]rune, n)
		for _, tail := range tails {
			copy(s, orig)
			if orig[n-2] == '?' {
				s[n-2] = tail[0]
			}
			if orig[n-1] == '?' {
				s[n-1] = tail[1]
			}

			ok := true
			now := s[n-1]
			cnt := 1
			if s[n-2] == now {
				cnt++
			}
			for i, r := range s {
				switch r {
				case '?':
					if (cnt < 3) || (s[(i+1)%n] == now) {
						cnt++
					} else {
						now = flip(now)
						cnt = 1
					}
					s[i] = now
				case now:
					cnt++
				default:
					if cnt < 3 {
						ok = false
					}
					now = r
					cnt = 1
				}
			}
			if now == s[0] {
				cnt++
				if now == s[1] {
					cnt++
				}
			}
			if cnt < 3 {
				ok = false
			}

			if ok {
				return true
			}
		}
		return false
	}

	t := sc.getInt()
	for i := 0; i < t; i++ {
		n := sc.getInt()
		s := sc.getRunes()
		ans := "No"
		if f(n, s) {
			ans = "Yes"
		}
		wr.println(ans)
	}
}

// 入出力の準備(sc, wr)
func main() {
	sc := &myScanner{bufio.NewScanner(os.Stdin)}
	wr := &myWriter{bufio.NewWriter(os.Stdout)}
	startBufSize := 4096
	maxBufSize := math.MaxInt64
	sc.Buffer(make([]byte, startBufSize), maxBufSize)
	sc.Split(bufio.ScanWords)

	solve(sc, wr)
	wr.Flush()
}

// useful funcs for slice
func makeInts(n, x int) []int {
	res := make([]int, n)
	for i := range res {
		res[i] = x
	}
	return res
}
func makeBools(n int, b bool) []bool {
	res := make([]bool, n)
	for i := range res {
		res[i] = b
	}
	return res
}
func countTrue(bs []bool) int {
	res := 0
	for _, b := range bs {
		if b {
			res++
		}
	}
	return res
}

// input
type myScanner struct {
	*bufio.Scanner
}

func (sc *myScanner) getInt() int {
	return sc._getIntOffset(0)
}
func (sc *myScanner) getInt2() (int, int) {
	return sc.getInt(), sc.getInt()
}
func (sc *myScanner) getInt3() (int, int, int) {
	return sc.getInt(), sc.getInt(), sc.getInt()
}
func (sc *myScanner) getInt4() (int, int, int, int) {
	return sc.getInt(), sc.getInt(), sc.getInt(), sc.getInt()
}
func (sc *myScanner) getInts(n int) []int {
	return sc._getIntsOffset(n, 0)
}
func (sc *myScanner) getIntZeroIndexed() int {
	return sc._getIntOffset(-1)
}
func (sc *myScanner) getIntsZeroIndexed(n int) []int {
	return sc._getIntsOffset(n, -1)
}
func (sc *myScanner) _getIntOffset(x int) int {
	res, err := strconv.Atoi(sc.getString())
	if err != nil {
		panic(err)
	}
	return res + x
}
func (sc *myScanner) _getIntsOffset(n, x int) []int {
	res := make([]int, n)
	for i := range res {
		res[i] = sc._getIntOffset(x)
	}
	return res
}
func (sc *myScanner) getString() string {
	sc.Scan()
	return sc.Text()
}
func (sc *myScanner) getStrings(n int) []string {
	res := make([]string, n)
	for i := range res {
		res[i] = sc.getString()
	}
	return res
}
func (sc *myScanner) getRunes() []rune {
	return []rune(sc.getString())
}
func (sc *myScanner) getFloat() float64 {
	res, err := strconv.ParseFloat(sc.getString(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (sc *myScanner) getFloats(n int) []float64 {
	res := make([]float64, n)
	for i := range res {
		res[i] = sc.getFloat()
	}
	return res
}

// output
type myWriter struct {
	*bufio.Writer
}

func (wr *myWriter) println(a ...interface{}) {
	fmt.Fprintln(wr, a...)
}
func (wr *myWriter) printf(format string, a ...interface{}) {
	fmt.Fprintf(wr, format, a...)
}

// simple math funcs for int
func max(as ...int) int {
	res := as[0]
	for _, a := range as {
		if res < a {
			res = a
		}
	}
	return res
}
func min(as ...int) int {
	res := as[0]
	for _, a := range as {
		if res > a {
			res = a
		}
	}
	return res
}
func chMax(a *int, b int) {
	*a = max(*a, b)
}

func chMin(a *int, b int) {
	*a = min(*a, b)
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func pow(a, n int) int {
	res := 1
	b := a
	for n > 0 {
		if n&1 > 0 {
			res *= b
		}
		n >>= 1
		b *= b
	}
	return res
}
func sum(s ...int) int {
	res := 0
	for _, v := range s {
		res += v
	}
	return res
}
func checkPrime(a int) bool {
	if a == 2 {
		return true
	} else if a%2 == 0 {
		return false
	}
	res := true
	for q := 3; q*q <= a; q += 2 {
		if a%q == 0 {
			res = false
			break
		}
	}
	return res
}
func toBInt(a int) *big.Int {
	return big.NewInt(int64(a))
}

/*
	めも
	int(64)の最大値: 9223372036854775807 = (2^63 - 1)
	19桁
	10^18 < 2^60 < 2^63 < 10^19
	10^19 はintだとオーバーフローする

	sliceの長さ: 10^8程度までは許される
	型によるがそれ以上は Out of memory の危険がある

	1~nの和: {n*(n+1)}/2
	(1~nの平均値)*n を式変形したもの
	先に掛け算をすることで必ず2で割り切れる
*/
0