본문 바로가기

알고리즘/C#

[C#] 백준 알고리즘 10809 : 알파벳 찾기

반응형


한 단어에서 각 알파벳이 처음 등장하는 위치를 찾아봅니다



  문제


알파벳 소문자로만 이루어진 단어 S가 주어진다.

각각의 알파벳에 대해서, 단어에 포함되어 있는 경우에는 처음 등장하는 위치를, 포함되어 있지 않은 경우에는 -1을 출력하는 프로그램을 작성하시오.






  소스


using System; namespace baekjoonTest { class Program { static void Main(string[] args) { string s = Console.ReadLine(); int[] arr = new int[26] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; int cnt = 0; for (int i = 0; i < s.Length; i++) { // ASCII 값 a(97) ~ z(122) for (int j = 97; j <= 122; j++) { if (arr[j - 97] == -1 && (int)Convert.ToChar(s.Substring(i, 1)) == j) { arr[j - 97] = i; } } } // 결과 출력 while (cnt < 26) { Console.Write("{0} ", arr[cnt]); cnt++; } } } }







  출처


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



반응형