結果

問題 No.38 赤青白ブロック
ユーザー izuru_matsuura
提出日時 2016-09-29 21:31:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 842 ms / 5,000 ms
コード長 1,449 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 177,712 KB
実行使用メモリ 23,040 KB
最終ジャッジ日時 2024-12-23 13:02:40
合計ジャッジ時間 7,224 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        auto i = vs.begin();
        os << "[" << *i;
        for (++i; i != vs.end(); ++i) os << " " << *i;
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int Kr, Kb;
    string S;
    void input() {
        cin >> Kr >> Kb;
        cin >> S;
    }

    bool check(const string& s) {
        int n = s.size();
        for (int i = 0; i < n; i++) {
            if (s[i] == 'W') continue;
            int k = (s[i] == 'R' ? Kr : Kb);
            if (i - k >= 0 && s[i - k] == s[i]) return false;
        }
        return true;
    }

    map<string, int> cache;
    int dfs(const string& s) {
        if (cache.count(s)) return cache[s];
        //cout << s << endl;
        int n = s.size();
        if (check(s)) return n;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == 'W') continue;
            ans = max(ans, dfs(s.substr(0, i) + s.substr(i + 1, n - i - 1)));
        }
        return cache[s] = ans;
    }

    void solve() {
        cout << dfs(S) << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}

0