Programming/백준

[1일 1백준 : 2941번] 크로아티아 알파벳

HyeunJae 2021. 1. 26. 22:52

여기서 decltype(strSize)은 size_t와 같다.

 

#include <iostream>
#include <string.h>

int compare(const char * str1,const char * str2)
{
	int compare = 0;
	size_t i;
	for (i = 0; str1[i] != '\0'; i++)
	{
		if (str1[i] != str2[i])
			break;
	}
	compare = str1[i] - str2[i];
	if (compare == 0) compare = 0;
	else if (compare < 0) compare = 1;
	else compare = -1;
	return compare;
}
/*
"c-"
"dz="
"d-"
"lj"
"nj"
"s="
"z="
*/
bool DetectCroatiaAlphabet(const char * alphabet)
{
	if (compare(alphabet, "c=") == 0
		|| compare(alphabet, "c-") == 0
		|| compare(alphabet, "dz=") == 0
		|| compare(alphabet, "d-") == 0
		|| compare(alphabet, "lj") == 0
		|| compare(alphabet, "nj") == 0
		|| compare(alphabet, "s=") == 0
		|| compare(alphabet, "z=") == 0)
		return true;
	return false;
}

int main(void)
{
	std::cin.tie(NULL);
	std::cout.tie(NULL);
	std::ios::sync_with_stdio(false);

	std::string str;
	std::cin >> str;
	int cnt = 0;
	size_t strSize = str.size();
	bool isContain = false;
	for (decltype(strSize) i = 0; i < strSize; i++)
	{
		isContain = false;
		for (decltype(strSize) j = i; j < strSize; j++)
		{
			std::string c = str.substr(i, (j - i) + 1);

			if (DetectCroatiaAlphabet(c.c_str())) {
				cnt++;
				i += strlen(c.c_str()) - 1;
				isContain = true;
				break;
			}
		}
		if (!isContain)
			cnt++;
	}

	std::cout << cnt << "\n";

	return 0;
}