結果

問題 No.1016 三目並べ
ユーザー tomatoma
提出日時 2020-04-03 21:56:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 2,716 bytes
コンパイル時間 1,714 ms
コンパイル使用メモリ 179,108 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-16 01:19:22
合計ジャッジ時間 2,674 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 64 ms
4,376 KB
testcase_08 AC 52 ms
4,376 KB
testcase_09 AC 86 ms
4,376 KB
testcase_10 AC 92 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tp3 = tuple<int, int, int>;
using Mat = vector<vector<ll>>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = { 0,1,0,-1 };
constexpr int dw[4] = { -1,0,1,0 };
bool isin(const int H, const int W, const int h, const int w) {
    return 0 <= h && h < H && 0 <= w && w < W;
}
template<typename T>
T minch(T& l, T r) {
    return l = min(l, r);
}
template<typename T>
T maxch(T& l, T r) {
    return l = max(l, r);
}
template<typename T>
void output(const T& val) {
    cout << val << endl;
}
template<typename T>
void output(const vector<T>& vec, const bool newline = false) {
    for (const T& val : vec)cout << val << (newline ? '\n' : ' '); cout << endl;
}
template<typename T>
void output(const vector<vector<T>>& mat) {
    for (const auto& row : mat)output(row);
}
// ============ template finished ============

bool cont3(const string& s) {
    int cnt = 0;
    for (char c : s) {
        if (c == 'o')cnt++;
        else cnt = 0;
        if (cnt == 3)return true;
    }
    return false;
}
int reach(const string& s, const int near = -INF) {
    const int N = s.size();
    int cnt = 0;
    set<int> cand;

    if (near == -INF) {
        rep(i, N - 2) {
            map<char, int> mp;
            REP(j, i, i + 3)mp[s[j]]++;
            if (mp['o'] == 2 && mp['-'] == 1) {
                REP(j, i, i + 3)if (s[j] == '-')cand.insert(j);
            }
        }
    }
    else {
        REP(i, max(0, near - 10), min(N - 2, near + 10)) {
            map<char, int> mp;
            REP(j, i, i + 3)mp[s[j]]++;
            if (mp['o'] == 2 && mp['-'] == 1) {
                REP(j, i, i + 3)if (s[j] == '-')cand.insert(j);
            }
        }
    }
    return cand.size();
}
int flat(const string& s) {
    const int N = s.size();
    rep(i, N - 1) {
        if (s[i] == 'o'&&s[i + 1] == '-') {
            int l = i;
            i += 2;
            while (i < N && s[i] == '-') i++;
            int r = i;
            if (r < N && s[r] == 'o' && (r - l - 1) % 2 == 1)return true;
        }
    }
    return false;
}
bool solve(const int N, string& s) {
    if (cont3(s) || reach(s) >= 1)return true;
    rep(i, N) {
        char& c = s[i];
        if (c == '-') {
            c = 'o';
            if (reach(s, i) >= 2)return true;
            c = '-';
        }
    }
    return flat(s);
}

int main()
{
    int T;
    cin >> T;
    while (T--) {
        int N;
        string s;
        cin >> N >> s;
        output(solve(N, s) ? "O" : "X");
    }
    return 0;
}
0