[BOJ]2941-크로아티아 알파

yoon_H·2024년 10월 9일

BOJ

목록 보기
106/110

2941

탐색을 어떻게 해야할지 고민했다.
조건과 분기점이 많아지면 쉽게 피로를 느끼는 습관을 고치는 게 좋을 듯.

성공코드

#include <iostream>
#include <string>
#include <vector>
using namespace std;


string letters[8] = {"dz=","c=", "c-", "d-", "lj", "nj", "s=", "z="};

int main()
{
	cin.tie(NULL);
	cout.tie(NULL);
	ios::sync_with_stdio(false);
	
	string str;

	cin >> str;

	int idx = 0;					// 시작 위치
	int cnt = 0;					// 총 단어 수
	const int size = str.size();	// 단어 길이
	const int length = 8;			// 크로아티아 알파벳 수

	while (idx < size)
	{
		string word = str.substr(idx, 2);

		int flag = false;	// 2글자 크로아티아 알파벳 판별

		for (int i = 1; i < length; i++)
		{
			if (word == letters[i])
			{
				idx += 2;
				cnt += 1;
				flag = true;
				break;
			}
		}

		if (flag)
		{
			continue;
		}
		else
		{
			if (idx + 2 >= size)	// 마지막인 1~2글자일 때
			{
				cnt += size-idx;
				break;
			}
			else
			{
				word += str[idx + 2];	// 3글자

				if (word == letters[0])	// dz= 판별
				{
					idx += 3;
					cnt += 1;
					continue;
				}
				else // 아니면 다음 글자부터 2글자
				{
					cnt += 1;
					idx += 1;
					continue;
				}
			}
		}
	}

	cout << cnt;


}

0개의 댓글