[BAEKJOON] 백준 5365: Decoder (C#)

2024. 5. 28. 23:03IT/BaekJoon

문제 링크

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

 

 

문제

All the Sith messages are sent using a complex coding scheme. You have cracked their code and must write a program to decode coded messages. Their code works as follows. Each word in the coded message represents one letter in the decoded message. Use the first letter of the first word and for each subsequent word use the nth letter where n is the length of the previous word. If the previous word is longer than the current word the current word represents a “space” (i.e., a blank space).

Here are two example:

 

 

입력

A positive integer, n, on the first line. After the first line there will be n words that represent the coded message.

 

 

출력

The decoded message on one line.

 

 

 

통과한 답안

using System.Text;

namespace _5365
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string[] inputs = Console.ReadLine().Split(' ');
            StringBuilder answer = new StringBuilder();
            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {
                    answer.Append(inputs[i][0]);
                }
                else
                {
                    if (inputs[i].Length >= inputs[i - 1].Length)
                    {
                        answer.Append(inputs[i][inputs[i - 1].Length - 1]);
                    }
                    else
                    {
                        answer.Append(" ");
                    }
                }
            }

            Console.WriteLine(answer.ToString());
        }
    }
}

 

주어진 문장을 조건에 맞춰서 해독하는 문제이다.

 

조건은 n으로 주어지는 단어들에 대해서

첫 단어는 첫 글자를 취하고

그 이후의 단어에서는 이전 단어의 길이번째에 있는 단어를 취한다.

(단, 현재 단어의 길이가 이전 단어보다 짧다면 공백으로 입력한다)

라는 조건으로 해독한 결과를 출력하면 된다.

 

조건에 맞게 첫 단어에서 처음 위치한 알파벳을 가져오고

그 이후에는 이전 단어의 길이에 위치한 알파벳을 가져오도록 구현했다.

 

문제 자체는 어렵지 않으나 문제를 해석하는 능력이 요구되는 문제이다.