답안 #151523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
151523 2019-09-03T13:37:27 Z leduykhongngu HicCup (FXCUP4_hiccup) C++17
0 / 100
12 ms 632 KB
#include <cstdio>
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;

string S = "";
int n;

bool Check(int x)
{
    stack<char> st;
    stack<int> cnt;
    int cur = 0; bool chk = true;
    for (int i = 0; i < n; ++i)
    {
//        cout << S[i] << ' ' << cur << endl;
        if (S[i] != '!')
        {
            st.push(S[i]);
            if(cur) cnt.push(cur);
            cur = 0;
        }
        else
        {
            if (chk) return false;
            if (st.size() && st.top() != 'H')
            {
                ++cur;
                st.push(S[i]);
            }
        }
        
        if (cur == x)
        {
            for (int i = 1; i <= x; ++i)
                if (st.top() == '!') st.pop();
                else return false;
            while (st.size() && st.top() == '!')
                st.pop();
            if (st.size() && st.top() == 'C') st.pop();
            else return false;
            if (st.size() && st.top() == 'H') st.pop();
            else return false;
            if (cnt.empty()) cur = 0;
            else 
            {
                cur = cnt.top();
                cnt.pop();
            }
        }
        chk = !((S[i] == 'C') || (S[i] == '!'));
    }
    return st.empty();
}

int HicCup(std::string tmp)
{
    for (int i = 0; i < tmp.length(); ++i)
    {
        S.push_back(tmp[i]);
        if (tmp[i] == 'C') S.push_back('!');
    }
    
    n = S.length();
    
//    cout << Check(1);
    
    int lef = 0, rig = n, ans = -1;
    while (lef <= rig)
    {
        int mid = lef + rig >> 1;
        if (Check(mid))
        {
            ans = max(ans, mid);
            lef = mid + 1;
        }
        else rig = mid - 1;
    }
    if (ans != -1) --ans;
    return ans;
}

Compilation message

hiccup.cpp: In function 'int HicCup(std::__cxx11::string)':
hiccup.cpp:59:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < tmp.length(); ++i)
                     ~~^~~~~~~~~~~~~~
hiccup.cpp:72:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid = lef + rig >> 1;
                   ~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 252 KB Output is correct
3 Correct 2 ms 292 KB Output is correct
4 Incorrect 12 ms 632 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 252 KB Output is correct
3 Correct 2 ms 292 KB Output is correct
4 Incorrect 12 ms 632 KB Output isn't correct
5 Halted 0 ms 0 KB -