結果

問題 No.2168 双頭ヒドラゲーム
ユーザー 👑 NachiaNachia
提出日時 2022-12-20 23:57:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 2,196 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 98,524 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 10:30:51
合計ジャッジ時間 2,585 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// (X|Y)Z = φ_X (Y) + Z
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <array>
#include <atcoder/modint>

using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

using Modint = atcoder::static_modint<998244353>;

struct Node {
    Node* p[3] = {};
};

Node* construct(string s, int& p){
    if(p == (int)s.size()) return nullptr;
    if(s[p] != '(') return nullptr;
    Node* res = new Node();
    p++;
    res->p[0] = construct(s, p);
    p++;
    res->p[1] = construct(s, p);
    p++;
    res->p[2] = construct(s, p);
    return res;
}

string reverse_construct(Node* p){
    if(!p) return "";
    return "(" + reverse_construct(p->p[0]) + "|" + reverse_construct(p->p[1]) + ")" + reverse_construct(p->p[2]);
}

#include <map>
map<pair<Node*, Node*>, bool> mem;

Node* phi(Node* x, Node* y){
    return new Node{ x, y, nullptr };
}

bool lt(Node *l, Node *r){
    if(!r) return false;
    if(!l) return true;
    if(mem.count(make_pair(l,r))) return mem[make_pair(l,r)];
    auto ltp = [](pair<Node*, Node*> l, pair<Node*, Node*> r) -> bool {
        if(lt(l.first, r.first)){
            return lt(l.second, phi(r.first, r.second));
        }
        if(lt(r.first, l.first)){
            return lt(phi(l.first, l.second), r.second);
        }
        return lt(l.second, r.second);
    };
    vector<pair<Node*, Node*>> L, R;
    rep(x,2){
        while(l){
            auto nx = make_pair(l->p[0], l->p[1]);
            while(L.size() && ltp(L.back(), nx)) L.pop_back();
            L.push_back(nx);
            l = l->p[2];
        }
        swap(l,r); swap(L,R);
    }
    return mem[make_pair(l,r)] = lexicographical_compare(L.begin(), L.end(), R.begin(), R.end(), ltp);
}


int main(){
    string chars = ")|(";
    string T0, T1; cin >> T0 >> T1;
    int p0 = 0, p1 = 0;
    auto t0 = construct(T0, p0);
    auto t1 = construct(T1, p1);
    assert(reverse_construct(t0) == T0);
    assert(reverse_construct(t1) == T1);
    if(lt(t1, t0)) cout << "0\n"; else cout << "1\n";
    return 0;
}


0