kunkunwoo@blog:~$ 거누권의 생각깜지

백준 2941번 _ 크로아티아 알파벳

· 배운 것 · 5분 읽기

https://www.acmicpc.net/problem/2941

2941번: 크로아티아 알파벳

깔끔히 푸는 방법이 있을까 하고 여러방면으로 생각해봤는데 도저히 생각나지 않아 노가다성이 짙게 짰다. 부끄럽다ㅠ

총 두번풀었는데,
첫번째 방법은 크로아티아 알파벳이 가진 문자가 1개인 경우, 2개인 경우, 3개인 경우를 다 따로 조건문을 만들었다. 1개인 경우 0, 2개는 2, 3개는 3 이렇게 return하게 해서 cnt, value, str.size()를 조합해서 풀었다.
answer = cnt + (str.size() - value) 이렇게

두번째 방법은 첫번째 방법이 솔직히 너무 부끄러워서 조금 고쳤다. 거의 같긴 하지만 알파벳을 세는 부분을 조금 고쳤는데 첫번째의 이상한 방법보단 이해하기 쉬울거라고 생각한다.


전체코드

첫번째 풀이

#include <iostream>
#include <algorithm>

using namespace std;

string str;

int isAlphabet(int idx)
{
	//남은 글자가 2개도 안남았을때
	if (idx >= str.size() - 1)
		return 0;

	char ch = str[idx];
	char ch2 = str[idx + 1];
	if (ch == 'c')
	{
		if (ch2 == '=')
			return 2;
		else if (ch2 == '-')
			return 2;
	}
	else if (ch == 'd')
	{
		if (ch2 == '-')
			return 2;
		else if(ch2 == 'z')
		{
			if (idx >= str.size() - 2)
				return 0;

			char ch3 = str[idx + 2];
			if (ch3 == '=')
				return 3;
		}
	}
	else if (ch == 'l')
	{
		if (ch2 == 'j')
			return 2;
	}
	else if (ch == 'n')
	{
		if (ch2 == 'j')
			return 2;
	}
	else if (ch == 's')
	{
		if (ch2 == '=')
			return 2;
	}
	else if (ch == 'z')
	{
		if (ch2 == '=')
			return 2;
	}

	return false;
}

int main()
{
	cin >> str;

	int cnt = 0;
	int value = 0;
	for (int i = 0; i < (int)str.size();)
	{
		int tmp =isAlphabet(i);
		if (tmp == 0)
		{
			i++;
			continue;
		}
		cnt++;
		value += tmp;
		i += tmp;
	}
	int answer = cnt + (str.size() - value);

	cout << answer;
	return 0;
}

두번째 코드

#include <iostream>
#include <string>

using namespace std;

int main() 
{
    string str;
    cin >> str;
    str.append("           ");

    int cnt = 0;
    int i = 0;
    while(1)
    {
        char word = str[i];
        if (word == ' ')
            break;

        if (word == 'n' || word == 'l')
        {
            if (str[i + 1] == 'j')
            {
                cnt ++;
                i += 2;
                continue;
            }
        }
        else if (word == 'c' || word == 's' || word == 'z')
        {
            if (str[i + 1] == '=' || str[i + 1] == '-')
            {
                cnt++;
                i += 2;
                continue;
            }
        }
        else if (word == 'd')
        {
            if (str[i + 1] == '-')
            {
                cnt++;
                i += 2;
                continue;
            }
            if (str[i + 1] == 'z' && str[i + 2] == '=')
            {
                cnt++;
                i += 3;
                continue;
            }
        }
		cnt++;
		i++;
    }

    cout << cnt;
}

후기

분명 쉬운데 쉽게 풀지 못하는 자신을 보며 실망했다. 내 생각보다 난 더 허접인가보다.