結果

問題 No.996 Phnom Penh
ユーザー toma
提出日時 2020-02-21 23:20:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,259 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 178,872 KB
実行使用メモリ 25,624 KB
最終ジャッジ日時 2024-10-09 02:23:54
合計ジャッジ時間 6,090 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 2 TLE * 1 -- * 3
権限があれば一括ダウンロードができます

ソースコード

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