[BOJ] 1406 에디터

Algorithm 2023. 1. 17. 18:59
반응형

힌트

커서를 기준으로 좌우 스택으로 나눈다 

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

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace _1406
{
    
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            string str = sr.ReadLine();
            Stack<char> lstack = new Stack<char>(str.ToArray());
            Stack<char> rstack = new Stack<char>();
            int m = int.Parse(sr.ReadLine());
            for (int i = 0; i < m; i++)
            {
                var arr = Array.ConvertAll(sr.ReadLine().Split(' '), char.Parse);
                    
                var cmd = arr[0];
                var word = (arr.Length > 1) ? arr[1] : '\0';

                switch (cmd)
                {
                    case 'L':
                        if(lstack.Count>0)
                            rstack.Push(lstack.Pop());
                        break;
                    case 'D':
                        if(rstack.Count>0)
                            lstack.Push(rstack.Pop());
                        break;
                    case 'B':
                        if (lstack.Count > 0)
                            lstack.Pop();
                        break;
                    case 'P':
                        lstack.Push(word);
                        break;
                }
            }

            while (lstack.Count > 0)
            {
                rstack.Push(lstack.Pop());
            }

            while (rstack.Count > 0)
            {
                sw.Write(rstack.Pop());
            }
            
            sr.Close();
            sw.Flush();
            sw.Close();

        }
    }
}

 

 

 

linkded list버전 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace _1406
{
    
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            string str = sr.ReadLine();
            StringBuilder sb = new StringBuilder();
            
            int M = int.Parse(sr.ReadLine());
            LinkedList<char> list = new LinkedList<char>(str.ToCharArray());
            list.AddLast('\0');
            
            LinkedListNode<char> node = list.Last;

            for (int i = 0; i < M; i++)
            {
                char[] s = Array.ConvertAll(sr.ReadLine().Split(' '), char.Parse);
                switch (s[0])
                {
                    case 'L':
                        if (node.Previous != null)
                            node = node.Previous;
                        break;

                    case 'D':
                        if (node.Next != null)
                            node = node.Next;
                        break;

                    case 'B':
                        if (node.Previous != null)
                        {
                            list.Remove(node.Previous);
                        }
                        break;

                    case 'P':
                        //Console.WriteLine("{0} {1}", node.Value, s[1]);
                        var n = list.AddBefore(node, s[1]);
                        //Console.WriteLine("---> {0}", n.Value);
                        break;
                }
            }

            //Console.WriteLine(list.Count);
            
            LinkedListNode<char> temp = list.First;
            while (temp.Value != '\0')
            {
                sb.Append(temp.Value);
                temp = temp.Next;
            }

            sw.Write(sb.ToString());
            
            sr.Close();
            sw.Flush();
            sw.Close();
            
        }
    }
}

 

 

stack이 약간 빠름

반응형

'Algorithm' 카테고리의 다른 글

[프로그래머스] 최소 직사각형  (0) 2023.01.17
[BOJ] 10845 큐  (0) 2023.01.17
[알고스팟] 록 페스티벌 C#  (0) 2023.01.17
BST (Binary Search Tree)  (0) 2023.01.16
LCRS Tree  (0) 2023.01.16
: