Submission #150429

#TimeUsernameProblemLanguageResultExecution timeMemory
150429Little Piplup (#200)HicCup (FXCUP4_hiccup)C++17
24 / 100
256 ms6320 KiB
#include "hiccup.h"
#include<bits/stdc++.h>
//using namespace std;
std::string str;
std::string ss;
bool check(int x)
{
	int excount = 0;
	ss.clear();
	if (x+2 > str.size())
		return false;
	int ssize = 0;
	int ccount = 0;
	for (auto it:str)
	{
		if (it=='!')
			excount++;
		else 
		{
			ss.push_back(it);
			if (it=='C')
				ccount++;
			ssize++;
		}
		
		if (ssize < 2)
		{
			//std::cout << ss << std::endl;
			continue;
		}
		bool ok = true;
		ok &= ss[ssize-2] == 'C';
		ok &= ss[ssize-1] == 'H';
		ok &= (excount >= x);
		if (ok)
		{
			ss.pop_back();
			ss.pop_back();
			ssize-=2;
			ccount--;
			excount -= x;
		}
		//std::cout << ss << std::endl;
	}
	for (auto it:ss)
	{
		if (it!='!')
			return false;
	}
	return true;
}
bool zerocheck()
{
	std::string tmpstr = "";
	for (auto it:str)
	{
		tmpstr.push_back(it);
		int sz = tmpstr.size();
		if (sz>=2)
		{
			while (tmpstr[sz-1] == 'H' && tmpstr[sz-2] == 'C')
			{
				tmpstr.pop_back();
				tmpstr.pop_back();
				sz-=2;
				while(tmpstr.back()=='!')
				{
					tmpstr.pop_back();
					sz--;
				}
			}
		}
		else continue;
	}
	//std::cout << "zerocheck : " << tmpstr << std::endl;
	return tmpstr.empty();
}
int HicCup(std::string S) {
	int N = S.size();
	if ( S[0] != 'H' ) return -1;
	str = S;
	std::reverse(str.begin(), str.end());
	//std::cout << str << std::endl;
	int lo = 0, hi = 1000000;
	if (!zerocheck())
		return -1;
	while(lo + 1 < hi)
	{
		int mid = lo+hi;
		mid /= 2;
		//printf("Checking %d\n",mid);
		if (check(mid))
		{
			//printf("OK %d\n",mid);
			lo = mid;
		}
		else 
		{
			//printf("NO %d\n",mid);
			hi = mid;
		}
	}
	return lo;
}

Compilation message (stderr)

hiccup.cpp: In function 'bool check(int)':
hiccup.cpp:10:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (x+2 > str.size())
      ~~~~^~~~~~~~~~~~
hiccup.cpp: In function 'int HicCup(std::__cxx11::string)':
hiccup.cpp:79:6: warning: unused variable 'N' [-Wunused-variable]
  int N = S.size();
      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...