문제번호: 4659
문제명: 비밀번호 발음하기
문제링크: https://www.acmicpc.net/problem/4659
문제내용과 예제 입/출력은 위 문제 링크에서 확인해 주시기 바랍니다.
오늘은 문자열을 활용한 구현문제를 풀어보았습니다. 해시셋 자료구조를 이용하여 모음,자음 알파벳을 저장하여 탐색속도를 O(1)로 보장하였습니다. 그 후에는 입력을 받은 알파벳을 하드코딩으로 조건 1,2,3을(문제 참고) 검사하여 참,거짓을 반환하고 그에 따른 양식을 출력하였습니다.
다음은 정답코드입니다.
public static void Main()
{
Console.OutputEncoding = Encoding.UTF8;
using var sr = new StreamReader(Console.OpenStandardInput());
using var sw = new StreamWriter(Console.OpenStandardOutput());
HashSet<char> Vowel = new HashSet<char>() {'a', 'e', 'i', 'o', 'u'};
HashSet<char> Consonant = new HashSet<char>() { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' };
while (true)
{
string input = sr.ReadLine();
if (input == "end") break;
bool answer = Verdict(input);
string isNot = answer ? "" : " not";
sw.WriteLine("<"+input+ "> is" + isNot + " acceptable.");
}
bool Verdict(string input)
{
bool First = false; // 모음(a,e,i,o,u) 하나를 반드시 포함하여야 한다.
bool Second = false; // 모음이 3개 혹은 자음이 3개 연속으로 오면 안 된다.
int stack = 0;
bool Third = false; // 같은 글자가 연속적으로 두번 오면 안되나, ee 와 oo는 허용한다.
char previousChar = ' ';
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if (Vowel.Contains(c) && First == false) First = true;
if (Vowel.Contains(previousChar) && Vowel.Contains(c) && Second == false)
stack++;
else if (Consonant.Contains(previousChar) && Consonant.Contains(c) && Second == false)
stack++;
else
stack = 0;
if (stack >= 2)
{
Second = true; // 스택이 3개 쌓이면 2번문항 참 판정
}
if (previousChar == c && c != 'e' && c != 'o' && Third == false)
Third = true;
previousChar = c;
}
//sw.WriteLine(First + ", " + Second + ", " + Third);
if (First == true && Second == false && Third == false)
return true;
else
return false;
}
sr.Close();
sw.Close();
}'알고리즘 풀이' 카테고리의 다른 글
| [C#] 백준 1149번 문제풀이 (0) | 2026.02.04 |
|---|---|
| [C#] 백준 16953번 문제풀이 (0) | 2026.02.03 |
| [C#] 백준 11728번 문제풀이 (0) | 2026.02.01 |
| [C#] 백준 13909번 문제풀이 (0) | 2026.01.31 |
| [C#] 백준 17478번 문제풀이 (3) | 2026.01.30 |