알고리즘/백준-실버

백준 4559번 비밀번호 발음하기(C++)

뜨거운 개발자 2023. 1. 6. 02:43

문제

정답 코드

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int is_have_small(char a)
{
	if (a == 'a' || a=='e' || a =='i' || a== 'o' || a=='u')
		return (1);
	// cout << "1";
	return (0);
}

int check_opt2(char a1,char a2,char a3)
{
	if (is_have_small(a1) && is_have_small(a2) && is_have_small(a3))
		return (1);
	else if (!is_have_small(a1) && !is_have_small(a2) && !is_have_small(a3))
		return (1);
	return (0);
}
int check_opt3(char a1 ,char a2)
{
	if (a1 == a2)
	{
		if (a1 != 'e' && a1 != 'o')
			return (1);
	}

	return (0);
}

int	check(string s)
{
	int size = s.size();
	int opt1 = 0;
	char first ,second , third;
	for(int i =0;i < size;i++)
	{
		if (opt1 == 0 && is_have_small(s[i]))
				opt1 = 1;
		if (i == 0)
			first = s[i];
		else if (i == 1)
		{
			second = s[i];
			if (check_opt3(first,second))
				return (1);
		}
		else if (i == 2)
		{
			third = s[i];
			if (check_opt2(first,second,third) ||check_opt3(first,second) || check_opt3(second,third))
				return (1);
		}
	 	else
		{
			first = second;
			second = third;
			third = s[i];
			if (check_opt2(first,second,third) ||check_opt3(first,second) || check_opt3(second,third))
				return(1);
		}
	}
	if (opt1 == 0)
		return (1);
	return (0);
}

int main()
{
	ios::sync_with_stdio(0);
	// cin.tie(0);

	string s;
	// cout <<"abc";
	while (1)
	{
		cin >>s;
		// cout <<s;
		if (s == "end")
			return(0);
		if (!check(s))
			cout << '<' << s <<'>' << " is acceptable.\n";
		else
			cout << '<' << s <<'>' << " is not acceptable.\n";
	}
}

문제 풀이의 흐름

  • end를 만날때까지 계속 입력을 받고
  • 각 문자열을 비교하는데
  • 하나라도 모음이 있는지 -조건 1
  • 두개가 같은게 나오는지 비교-조건2
  • 세개의 연속되는게 있는지 -조건 3
  • 이렇게 비교를 했다.
  • 이후 결과 값 리턴

주의 할 점

이 문제는 함수로 잘 짤 수 있는지를 보는 문제 같다. 아마 그냥 쭉 짜게 되면 헷갈려서 for문을 여러번 반복해야 할 것이다.

필요한 부분을 잘 잘라서 사용하면 되는 것 같다.

Cin.tie(0);

이걸 사용하니까 자꾸 출력이 안됐다.

cin.tie(0)는 cin과 cout의 묶음을 풀어준다. 따라서 순서가 사라진다. 알고리즘을 풀때는 화면에 바로 보이는 것이 중요하지 않기 때문에 cin과 cout의 묶음을 풀어주어 더 빠르게 만들 수 있다.

따라서 끝날때야 나온 것이다.

제출 할 때는 저걸 걸어주는게 테스트 할 때는 풀어주는게 좋은 것 같다.

반성 및 고찰

꼼꼼하게 생각을 하지 않아서 자꾸 틀렸다.

생각보다 정말 단순한 이유인 인덱스에서 틀려버리면 한참동안 디버깅을 하게 되기 때문에 꼭 집중해서 풀도록 하자.


Uploaded by N2T

728x90