結果

問題 No.2283 Prohibit Three Consecutive
ユーザー hongrockhongrock
提出日時 2023-04-28 21:33:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,358 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 201,208 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 23:13:36
合計ジャッジ時間 2,933 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int T, n;
string s;
int f[2][2], g[2][2];

bool ok(int a, int b){
  for(int i=0; i<2; ++i){
    for(int j=0; j<2; ++j)  f[i][j] = g[i][j] = 0;
  }
  f[a][b] = 1;
  for(int i=2; i<n; ++i){
    for(int j=0; j<2; ++j){
      for(int k=0; k<2; ++k)  g[j][k] = 0;
    }
    for(int j=0; j<2; ++j){
      for(int k=0; k<2; ++k){
        if(!f[j][k])  continue;
        for(int l=0; l<2; ++l){
          if(j == k && j == l)  continue;
          if(s[i] == '?' || s[i] == ('0' + l)){
            g[k][l] = 1;
          }
        }
      }
    }
    for(int j=0; j<2; ++j){
      for(int k=0; k<2; ++k){
        f[j][k] = g[j][k];
      }
    }
  }

  for(int i=0; i<2; ++i){
    for(int j=0; j<2; ++j){
      if(!f[i][j])  continue;
      if(i == j && i == a)  continue;
      if(j == a && j == b)  continue;
      return 1;
    }
  }
  return 0;
}

bool ok(){
  for(int i=0; i<2; ++i){
    if(s[0] != '?' && s[0] != ('0' + i)){
      continue;
    }
    for(int j=0; j<2; ++j){
      if(s[1] == '?' || s[1] == ('0' + j)){
        if(ok(i, j))  return 1; 
      }
    }
  }
  return 0;
}

int main(){
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

  cin >> T;
  while(T--){
    cin >> n >> s;
    if(ok()){
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  }

  return 0;
}
0