답안 #149747

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
149747 2019-09-01T07:05:09 Z 강한친구 대한육군(#3592, pichulia) HicCup (FXCUP4_hiccup) C++17
24 / 100
1000 ms 262144 KB
#include "hiccup.h"
static bool able(int& dep, std::string a, int x);
static bool dfs(int &dep, std::string a, int x, int state)
{
	//printf("%d %d %d ", dep, x, state);
	//if (dep < a.size())printf("%c", a[dep]);
	//else printf("\\");
	//printf("\n");
	if (dep == a.size())return (state == (x+2));
	if (state == x + 2)
	{
		return true;
	}
	if (state == 1) {
		while (a[dep] != 'C')
		{
			if (a[dep] == 'H')
			{
				if (!able(dep, a, x))
					return false;
			}
			else
			{
				dep++;
			}
		}
		if (a[dep] == 'C') {
			dep++;
			return dfs(dep, a, x, 2);
		}
	}
	else {
		while (a[dep] != '!')
		{
			if (a[dep] == 'H')
			{
				if (!able(dep, a, x))
					return false;
			}
			else
				return false;
		}
		if (a[dep] == '!')
		{
			dep++;
			return dfs(dep, a, x, state + 1);
		}
	}
}
static bool able(int& dep, std::string a, int x)
{
	//printf("-cur dep : %d\n", dep);
	if (a[dep] == 'H')
	{
		dep++;
		if (!dfs(dep, a, x, 1))
			return false;
	}
	else if (a[dep] == 'C')
	{
		return false;
	}
	return true;
}
static bool able2(int &dep, std::string a, int x)
{
	while (dep < a.size())
	{
		//printf("cur dep : %d\n", dep);
		if (a[dep] == 'H')
		{
			dep++;
			if (!dfs(dep, a, x, 1))
				return false;
		}
		else if(a[dep] == 'C')
			return false;
		else {
			dep++;
		}
	}
	return true;
}
int HicCup(std::string a) {
	int n = a.size();
	int i, j, k;
	for (i = j=k=0; i < n; i++) {
		if (a[i] == 'H') {
			k++; j = 0;
		}
		else if (a[i] == 'C') { k--; j = 1; if (k < 0)return -1; }
		else {
			if (j == 0)return -1;
		}
	}
	if (k > 0) return -1;
	int l = 1;
	int r = n;

	while (l < r) {
		int mid = (l + r) / 2;
		int dep = 0;
		//printf("try %d\n", mid);
		if (able2(dep, a, mid))
		{
			l = mid + 1;
		}
		else
			r = mid;
	}

	return l-1;
}

Compilation message

hiccup.cpp: In function 'bool dfs(int&, std::__cxx11::string, int, int)':
hiccup.cpp:9:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (dep == a.size())return (state == (x+2));
      ~~~~^~~~~~~~~~~
hiccup.cpp: In function 'bool able2(int&, std::__cxx11::string, int)':
hiccup.cpp:67:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while (dep < a.size())
         ~~~~^~~~~~~~~~
hiccup.cpp: In function 'bool dfs(int&, std::__cxx11::string, int, int)':
hiccup.cpp:49:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
3 Correct 6 ms 384 KB Output is correct
4 Correct 11 ms 1236 KB Output is correct
5 Correct 440 ms 29816 KB Output is correct
6 Correct 14 ms 3328 KB Output is correct
7 Correct 15 ms 3328 KB Output is correct
8 Correct 259 ms 20040 KB Output is correct
9 Correct 273 ms 19104 KB Output is correct
10 Correct 13 ms 3328 KB Output is correct
11 Correct 5 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
3 Correct 6 ms 384 KB Output is correct
4 Correct 11 ms 1236 KB Output is correct
5 Correct 440 ms 29816 KB Output is correct
6 Correct 14 ms 3328 KB Output is correct
7 Correct 15 ms 3328 KB Output is correct
8 Correct 259 ms 20040 KB Output is correct
9 Correct 273 ms 19104 KB Output is correct
10 Correct 13 ms 3328 KB Output is correct
11 Correct 15 ms 3328 KB Output is correct
12 Correct 15 ms 3328 KB Output is correct
13 Correct 14 ms 3328 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 14 ms 3328 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 6 ms 640 KB Output is correct
19 Execution timed out 1068 ms 262144 KB Time limit exceeded
20 Halted 0 ms 0 KB -