[C#] 백준 2941번 크로아티아 알파벳
2021. 12. 19. 01:04ㆍ코딩테스트 문제 풀이/[백준] 문자열
728x90
반응형
using System;
namespace _9
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
int count = 0;
for (int i = 0; i < input.Length; i++)
{
string check;
if (input[i] == 'c' || input[i] == 'd' || input[i] == 'l' || input[i] == 'n' || input[i] == 's' || input[i] == 'z')
{
if (i + 1 < (input.Length))
{
check = input[i].ToString() + input[i + 1].ToString();
if (check.Contains("c=") || check.Contains("c-") || check.Contains("d-") || check.Contains("lj") || check.Contains("nj") || check.Contains("s=") || check.Contains("z=")) //lj
{
i += 1;
}
else if (check.Contains("dz"))
{
if(i + 2 < (input.Length) && input[i+2].ToString() == "=")
i += 2;
}
count++;
}
else
count++;
}
else
count++;
}
Console.WriteLine(count);
}
}
}
내가 푼 방법은 크로아티아 알파벳은 c,d,l,n,s,z로 시작하는 문자열이길래 그럴 경우에 뒤에 오는 문자열이 무엇인지를 비교하여 count를 세는 방법으로 풀었다. 실제로 이렇게 풀때 이게 맞나 싶었는데 역시 아니나 다를까
Replace() 를 통해 특정 알파벳이 아무 문자열로 변환한 후, 문자열의 전체길이를 구하는 방식으로 풀더라
예를 들어, ljes=njak는 %%%ak 이런식으로 크로아티아 알파벳만 다른 문자로 바꿔주고 전체 문자열의 길이인 6을 출력하는 방식이다. Replace()를 배우게 되었다.
728x90
반응형
'코딩테스트 문제 풀이 > [백준] 문자열' 카테고리의 다른 글
[C#] 백준 1316번 그룹 단어 채커 (0) | 2021.12.19 |
---|---|
[C#] 백준 5622번 다이얼 (0) | 2021.12.19 |
[C#] 백준 2908번 상수 (0) | 2021.12.19 |
[C#] 백준 1152번 단어의 개수 (0) | 2021.12.19 |
[C#] 백준 2675번 문자열 반복 (0) | 2021.12.19 |