본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 10809 알파벳 찾기

by 왹져박사 2023. 9. 7.
728x90

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

 

10809번: 알파벳 찾기

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출

www.acmicpc.net

알파벳의 아스키코드를 이용하였다. 

using System;
using System.Text;

namespace B10809
{
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();

            string s = sr.ReadLine();
            int[] pos = new int[26];

            //모든 알파벳
            for(int i=0; i<26; i++)
            {
                pos[i] = -1;
                if (i!=0) sb.Append(" ");

                //주어진 단어의 알파벳
                for(int j = 0; j < s.Length; j++)
                {
                    //글자의 j번째 숫자와 i가 같으면
                    if (s[j] - 97 == i && pos[i] == -1)
                    {
                        pos[i] = j;
                        break;
                    }
                }
                sb.Append(pos[i]);
            }
            sw.WriteLine(sb.ToString());
            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

 

실버4로 승급했다!!

당분간은 프로젝트에 더 집중해야겠다!

728x90

'알고리즘 > 백준 BOJ' 카테고리의 다른 글

[ BOJ/C# ] 1620 나는야 포켓몬 마스터 이다솜  (0) 2023.09.09
[ BOJ/C# ] 8958 OX퀴즈  (0) 2023.09.09
[ BOJ/C# ] 10773 제로  (0) 2023.09.07
[ BOJ/C# ] 1874 스택 수열  (0) 2023.09.05
[ BOJ/C# ] 2164 카드2  (0) 2023.09.05