제출 #150786

#제출 시각아이디문제언어결과실행 시간메모리
150786오리버스부릉부릉 (#200)HicCup (FXCUP4_hiccup)C++17
24 / 100
24 ms3328 KiB
#include "hiccup.h"
#include <stack>
#include <vector>
using namespace std;

int HicCup(std::string S) {
	bool flag = false;
	int N = S.size();
	int sp, wp;
	stack<int> sta;
	for (int i = 0; i < N; i++) {
		if (S[i] == '!') {
			if (!flag) return -1;
			continue;
		}
		if (S[i] == 'H') {
			sta.push(1);
		}
		else {
			if (sta.empty()) return -1;
			else {
				sta.pop();
				flag = true;
			}
		}
	}
	if (!sta.empty()) return -1;

	int ret = 0;
	int l = 1, r = 1000000, mid;
	while (l <= r) {
		bool suc = true;
		mid = (l + r) / 2;
		sp = wp = 0;
		bool flag = false;
		for (int i = 0; i < N; i++) {
			if (S[i] == '!') {
				if (!flag && (sta.empty() || sta.top() == -1)) {
					suc = false;
					break;
				}
				else if(!sta.empty() && sta.top() > 0){
					sta.top()--;
					if (sta.top() == 0) sta.pop();
				}
			}
			else if (S[i] == 'H') {
				sta.push(-1);
			}
			else {
				if (sta.empty() || sta.top() != -1) {
					suc = false;
					break;
				}
				else {
					sta.pop();
					sta.push(mid);
					flag = true;
				}
			}
		}
		
		if (suc && sta.empty()) {
			ret = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}

	return ret;
}

컴파일 시 표준 에러 (stderr) 메시지

hiccup.cpp: In function 'int HicCup(std::__cxx11::string)':
hiccup.cpp:9:6: warning: variable 'sp' set but not used [-Wunused-but-set-variable]
  int sp, wp;
      ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...