結果

問題 No.996 Phnom Penh
ユーザー tomatoma
提出日時 2020-02-21 23:20:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,259 bytes
コンパイル時間 2,251 ms
コンパイル使用メモリ 176,336 KB
実行使用メモリ 25,620 KB
最終ジャッジ日時 2024-04-17 09:21:34
合計ジャッジ時間 6,508 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,216 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 47 ms
25,496 KB
testcase_14 AC 44 ms
25,488 KB
testcase_15 AC 45 ms
25,492 KB
testcase_16 AC 45 ms
25,492 KB
testcase_17 AC 44 ms
25,620 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 2 ms
5,376 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

bool proc1(stack<char>& q) {
    if (q.size() < 5)return false;
    string s;
    rep(i, 5) {
        s += q.top();
        q.pop();
    }
    if (s == "phnom") {
        s = "hnep";
        for (char c : s) {
            q.push(c);
        }
        return true;
    }
    else {
        reverse(s.begin(), s.end());
        for (char c : s) {
            q.push(c);
        }
        return false;
    }
}

bool proc2(stack<char>& q) {
    bool res = false;
    stack<char> l;
    while (!q.empty()) {
        char c = q.top(); q.pop();
        if (c == 'h') {
            res = true;
        }
        else {
            l.push(c);
        }
    }
    // reverse
    while (!l.empty()) {
        char c = l.top(); l.pop();
        if (c == 'e') {
            res = true;
            q.push('h');
        }
        else {
            q.push(c);
        }
    }
    return res;
}

pair<int, int> analyze(stack<char>& q) {
    int n1 = 0, n2 = 0;
    int cnt = 0;
    while (true) {
        // try n1
        cnt++;
        while (proc1(q)) {
            n1++;
            cnt = 0;
        }
        if (cnt >= 2)break;
        // try n2
        if (proc2(q)) {
            n2++;
            cnt = 0;
        }
        else {
            cnt++;
        }
        if (cnt >= 2)break;
    }
    return { n1,n2 };
}

int main()
{
    string s;
    cin >> s;

    vector<stack<char>> qs;
    while (!s.empty()) {
        stack<char> q;
        while (!s.empty() && s.back() != 'p') {
            q.push(s.back());
            s.pop_back();
        }
        if (!s.empty()) {
            q.push('p');
            s.pop_back();
            qs.push_back(q);
        }
    }

    int n1 = 0, n2 = 0;
    for (auto& q : qs) {
        int tn1, tn2;
        tie(tn1, tn2) = analyze(q);
        n1 += tn1;
        n2 = max(n2, tn2);
        //cout << tn1 << " " << tn2 << endl;
    }
    cout << n1 + n2 << endl;
    //cout << endl;
    //cout << n1 << " " << n2 << endl;
    // debug
    /*
    for (auto& q : qs) {
        while (!q.empty())cout << q.top(), q.pop();
        cout << endl;
    }
    */
    return 0;
}
0