Submission #149596

#TimeUsernameProblemLanguageResultExecution timeMemory
149596----MIT합격선---- (#200)HicCup (FXCUP4_hiccup)C++17
100 / 100
435 ms19768 KiB
#include "hiccup.h"
#include <utility>
#include <stack>
#include <cstdlib>
using namespace std;

struct state {
	int a, c, g; // {^, H, C}, !count, ghost
};

typedef pair<char, state> pcs;

int n;

bool check(const string& S, int x){

	stack<pcs> T;
	state now={0,0,0};
	T.push({'^', now});
	for(int i=0; i<n; i++){
		if(S[i]=='H'){
/*			if(now.a==0){
				if(now.g) while(T.top().first!='^') T.pop();
				if(T.top().first!='^') return false;
			}
			else if(now.a==1){
				if(now.g) while(T.top().first!='H') T.pop();
				if(T.top().first!='H') return false;
			}*/
//			now = T.empty() ? {0,0,0} : T.top().second;
			now.a = 1, now.c = 0, now.g = 0;
		}
		if(S[i]=='C'){
			if(now.a != 1) return false;
			now.a = 2, now.c = 0, now.g = 0;
		}
		if(S[i]=='!'){
			if(now.a != 2){
				if(!now.g) return false;
				continue;
			}
			now.c++;
		}
		T.push({S[i], now});

		if(now.c>x) exit(42);
		if(now.c>=x && now.a==2){
			for(int j=0; j<x+2; j++) T.pop();
			now = T.top().second;
			now.g = 1;
		}
	}
	return now.a==0;
}

int HicCup(std::string S) {
	n = S.size();
	int s = 0, e = 1000000;
	while(s<e){
		int mid = (s+e+1)/2;
		if(check(S, mid)) s = mid;
		else e = mid-1;
	}
	return check(S, s) ? s : -1;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...