Submission #64118539


Source Code Expand

#include <iostream>
#include <random>
using namespace std;

bool check(string t, bool rev, char start){
  string s = t;
  char cur = start;
  if(rev){
    for(int i = s.size()-1; 0 <= i; i--){
      if(s[i] == '?'){
        s[i] = cur;
        if(cur == '(')cur = ')';
        else if(cur == ')')cur = '(';
      }
    }
  }else{
    for(int i = 0; s.size() > i; i++){
      if(s[i] == '?'){
        s[i] = cur;
        if(cur == '(')cur = ')';
        else if(cur == ')')cur = '(';
      }
    }
  }
  int nw = 0;
  for(int i = 0; s.size() > i; i++){
    if(s[i] == '('){
      nw += 1;
    }else{
      nw -= 1;
    }
    if(nw < 0){
      return true; //second win
    }
  }
  return false; //first win
}

bool solve(){
  string s;cin>>s;
  int l = 0;
  int r = 0;
  int q = 0;
  for(int i = 0; s.size() > i; i++){
    if(s[i] == ')'){
      r++;
    }else if(s[i] == '('){
      l++;
    }else{
      q++;
    }
  }
  if(q == 0){
    int nw = 0;
    for(int i = 0; s.size() > i; i++){
      nw += (s[i] == '(' ? 1 : -1); 
      if(nw < 0){
        return true;
      }
    }
    if(nw != 0){
      return true;
    }
    return false;
  }
  if(q%2 == 0 || (s[0] == '?' && s[s.size()-1] == '?') || (abs(l-r) > 1)){
    return true;
  }

  if(l > r){
    for(int i = s.size()-1; 0 <= i; i--){
      if(s[i] == '?'){
        s[i] = ')';
        break;
      }
    }
  }else{
    for(int i = 0; s.size() > i; i++){
      if(s[i] == '?'){
        s[i] = '(';
        break;
      }
    }
  }
  bool ret = check(s, false, '(');
  ret |= check(s, false, ')');
  ret |= check(s, true, '(');
  ret |= check(s, true, ')');
  return ret;

}
int main(){
  int t;cin>>t;
  for(int i = 0; t > i; i++){
    if(solve()){
      cout << "Second" << endl;
    }else{
      cout << "First" << endl;
    }
  }
}

Submission Info

Submission Time
Task R - Bracket Game
User CleyL
Language C++ 20 (gcc 12.2)
Score 100
Code Size 1898 Byte
Status AC
Exec Time 121 ms
Memory 6264 KiB

Compile Error

Main.cpp: In function ‘bool check(std::string, bool, char)’:
Main.cpp:17:29: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
   17 |     for(int i = 0; s.size() > i; i++){
      |                    ~~~~~~~~~^~~
Main.cpp:26:27: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
   26 |   for(int i = 0; s.size() > i; i++){
      |                  ~~~~~~~~~^~~
Main.cpp: In function ‘bool solve()’:
Main.cpp:44:27: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
   44 |   for(int i = 0; s.size() > i; i++){
      |                  ~~~~~~~~~^~~
Main.cpp:55:29: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
   55 |     for(int i = 0; s.size() > i; i++){
      |                    ~~~~~~~~~^~~
Main.cpp:78:29: warning: comparison of integer expressions of different signedness: ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
   78 |     for(int i = 0; s.size() > i; i++){
      |                    ~~~~~~~~~^~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 72
Set Name Test Cases
Sample 00_sample_00
All 00_sample_00, 01_test_00, 01_test_01, 01_test_02, 01_test_03, 01_test_04, 01_test_05, 01_test_06, 01_test_07, 01_test_08, 01_test_09, 01_test_10, 01_test_11, 01_test_12, 01_test_13, 01_test_14, 01_test_15, 01_test_16, 01_test_17, 01_test_18, 01_test_19, 01_test_20, 01_test_21, 01_test_22, 01_test_23, 01_test_24, 01_test_25, 01_test_26, 01_test_27, 01_test_28, 01_test_29, 01_test_30, 01_test_31, 01_test_32, 01_test_33, 01_test_34, 01_test_35, 01_test_36, 01_test_37, 01_test_38, 01_test_39, 01_test_40, 01_test_41, 01_test_42, 01_test_43, 01_test_44, 01_test_45, 01_test_46, 01_test_47, 01_test_48, 01_test_49, 01_test_50, 01_test_51, 01_test_52, 01_test_53, 01_test_54, 01_test_55, 01_test_56, 01_test_57, 01_test_58, 01_test_59, 01_test_60, 01_test_61, 01_test_62, 01_test_63, 01_test_64, 01_test_65, 01_test_66, 01_test_67, 01_test_68, 01_test_69, 01_test_70
Case Name Status Exec Time Memory
00_sample_00 AC 1 ms 3532 KiB
01_test_00 AC 121 ms 3508 KiB
01_test_01 AC 108 ms 3496 KiB
01_test_02 AC 109 ms 3444 KiB
01_test_03 AC 108 ms 3508 KiB
01_test_04 AC 108 ms 3532 KiB
01_test_05 AC 106 ms 3468 KiB
01_test_06 AC 106 ms 3500 KiB
01_test_07 AC 3 ms 3444 KiB
01_test_08 AC 49 ms 3472 KiB
01_test_09 AC 35 ms 3520 KiB
01_test_10 AC 27 ms 3544 KiB
01_test_11 AC 22 ms 3600 KiB
01_test_12 AC 21 ms 3568 KiB
01_test_13 AC 18 ms 3636 KiB
01_test_14 AC 17 ms 3528 KiB
01_test_15 AC 18 ms 3492 KiB
01_test_16 AC 54 ms 3472 KiB
01_test_17 AC 38 ms 3532 KiB
01_test_18 AC 30 ms 3540 KiB
01_test_19 AC 25 ms 3484 KiB
01_test_20 AC 24 ms 3452 KiB
01_test_21 AC 23 ms 3484 KiB
01_test_22 AC 24 ms 3536 KiB
01_test_23 AC 26 ms 3512 KiB
01_test_24 AC 18 ms 3632 KiB
01_test_25 AC 35 ms 3600 KiB
01_test_26 AC 18 ms 3612 KiB
01_test_27 AC 34 ms 3956 KiB
01_test_28 AC 17 ms 4736 KiB
01_test_29 AC 36 ms 4928 KiB
01_test_30 AC 33 ms 5168 KiB
01_test_31 AC 37 ms 5084 KiB
01_test_32 AC 16 ms 5248 KiB
01_test_33 AC 37 ms 6264 KiB
01_test_34 AC 17 ms 5192 KiB
01_test_35 AC 37 ms 6264 KiB
01_test_36 AC 17 ms 5200 KiB
01_test_37 AC 37 ms 6204 KiB
01_test_38 AC 23 ms 3524 KiB
01_test_39 AC 20 ms 3692 KiB
01_test_40 AC 18 ms 3484 KiB
01_test_41 AC 17 ms 3540 KiB
01_test_42 AC 17 ms 3544 KiB
01_test_43 AC 17 ms 3696 KiB
01_test_44 AC 16 ms 3604 KiB
01_test_45 AC 13 ms 3636 KiB
01_test_46 AC 21 ms 3600 KiB
01_test_47 AC 12 ms 3724 KiB
01_test_48 AC 20 ms 3840 KiB
01_test_49 AC 13 ms 4824 KiB
01_test_50 AC 21 ms 4940 KiB
01_test_51 AC 12 ms 4692 KiB
01_test_52 AC 21 ms 5076 KiB
01_test_53 AC 13 ms 5392 KiB
01_test_54 AC 22 ms 6196 KiB
01_test_55 AC 12 ms 5228 KiB
01_test_56 AC 21 ms 6244 KiB
01_test_57 AC 13 ms 5268 KiB
01_test_58 AC 21 ms 6220 KiB
01_test_59 AC 15 ms 3652 KiB
01_test_60 AC 15 ms 3636 KiB
01_test_61 AC 16 ms 3856 KiB
01_test_62 AC 15 ms 3680 KiB
01_test_63 AC 15 ms 4828 KiB
01_test_64 AC 16 ms 4704 KiB
01_test_65 AC 15 ms 4772 KiB
01_test_66 AC 15 ms 4728 KiB
01_test_67 AC 16 ms 5144 KiB
01_test_68 AC 15 ms 5224 KiB
01_test_69 AC 15 ms 5260 KiB
01_test_70 AC 15 ms 5228 KiB