한 단어에서 각 알파벳이 처음 등장하는 위치를 찾아봅니다
문제
알파벳 소문자로만 이루어진 단어 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++;
}
}
}
}
출처 |
'알고리즘 > C#' 카테고리의 다른 글
[C#] 백준 알고리즘 9012 : 괄호 (0) | 2018.08.06 |
---|---|
[C#] 백준 알고리즘 2748 : 피보나치 수 2 (0) | 2018.08.03 |
[C#] 백준 알고리즘 2557 : Hello World (0) | 2018.07.31 |