Algorithm/BOJ

[BOJ]1343 폴리오미노

Game Client Lee Hwanguk 2023. 2. 10. 22:33

#그리디

#문자열

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

 

1343번: 폴리오미노

첫째 줄에 사전순으로 가장 앞서는 답을 출력한다. 만약 덮을 수 없으면 -1을 출력한다.

www.acmicpc.net

https://learn.microsoft.com/ko-kr/dotnet/api/system.string.replace?view=net-7.0 

 

String.Replace 메서드 (System)

현재 문자열에서 발견되는 지정된 유니코드 문자 또는 String을 모두 지정된 다른 유니코드 문자 또는 String으로 바꾼 새 문자열을 반환합니다.

learn.microsoft.com

https://learn.microsoft.com/ko-kr/dotnet/api/system.string.contains?view=net-7.0#system-string-contains(system-char) 

 

String.Contains 메서드 (System)

이 문자열 내에서 지정한 문자가 표시되는지를 나타내는 값을 반환합니다.

learn.microsoft.com

# X들을 모두 AAAA와 BB로 채운 후, 남는칸에 BB 로 채운다 ex) XXXX->AAAA,BBBB (사전순으로 나열)

*입력받은 input을 먼저 AAAA로 Replace해본다 ("XXXX"->"AAAA") ->string A

*Replace 처리한 변수에서 'XX'를 'BB'로 Replace ("XX"->"BB")->string B

*if문안에서 contain으로 짝수가아닌 X를 찾자 (짝수의 X라면 AAAA,또는 BB로 나누어 떨어졌기 때문)

*Replace되지 않은 X가 있다면 -1 출력, 나누어 떨어졌다면 stirng B출력

*사전 순으로 나열해야하기 때문에 List에 할당후 Sort하고 출력

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ConsoleApp19
{
    internal class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
            StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
            // AAAA , BB 
            // . X 
            string input = sr.ReadLine();
            string A = input.Replace("XXXX", "AAAA");
            string B = A.Replace("XX", "BB");
            char[] Bchars = B.ToCharArray();

            List<char>list = new List<char>();            
            //사전 순으로 출력하기 위해 B를 List에 할당 후 sort 
            for(int i = 0; i < Bchars.Length; i++)
            {
                list.Add(Bchars[i]);

            }
            if(list.Contains('X'))
            {
                sw.WriteLine(-1);
            }
            else
            {
                foreach (var output in list)
                {
                    sw.Write(output);
                }
            }
            sr.Close();
            sw.Close();
        }
    }
}