티스토리 뷰

삽질 오지게 한 문제...


시도랄것도 전에, 입력값 분리하고 int 리스트로 만들려고 했는데 그러면 처리할게 너무 많았던건지 오류나고 난리였다.

하지만 애초에 계산도 비교도 하지 않기 떄문에 int형으로 변환 할 필요 자체가 없었음(ㅎㅎ빡췸)





시도 1) Reverse는 진짜 뒤집고 Delete는 진짜 삭제했다 : 시간초과

시도 2) Reverse는 bool isReverse 여부만 조정, Delete는 진짜 삭제했다 : 시간초과

시도 3) Reverse는 bool값, Delete는 index front, back만 조정 : 틀렸습니다(??)


reverse와 delete 매커니즘 :




출력방식 : 

출력할 값 마다 뒤에 ,를 붙여 출력하게 했고, 모든 출력이 끝난 후 마지막이 ,이라면 ]로 그 값을 대체하였다.

그 외의 경우는 []를 출력해야하는 경우이므로 (값이 없는 경우) ,로 끝나지 않기 때문에 ]를 추가한다.



시도 3) 에서 몇가지 해결해서 시도 5~6까지 갔지만 자꾸 틀렸습니다가 떴다...

결국 분노가 머리끝까지 차올라서 낭나해! 하고 던지고 며칠 뒤에 다시 코드를 들여봤다.

null을 ""로 바꾸고 해결됐다 (ㅡㅡ) 1분만에 해결함. 역시 시간은 최고의 답이다.


이 문제에서 R과 D 기능을 충실히 시간 내로 구현했다 해도 걸릴법한 구멍이 몇 개 있다.


1. 입력값 : []에 대한 반응 (R과 D)

2. error 출력 후 해당 값에 대한 출력이 멈추는지 확인

3. []가 출력값일 경우 잘 출력되는지


이 정도가 떠오른다. 아마 더 있었을텐데 며칠 지나서 기억안남. 


  
            BufferedStream bs = new BufferedStream(Console.OpenStandardInput());
            StreamReader sr = new StreamReader(bs);
            StringBuilder sb = new StringBuilder();
            int count = int.Parse(sr.ReadLine());
            int frontIndex;
            int backIndex;
            bool keepGoing = true;
            bool isReverse = false;
            char[] commandLine = new char[100000];
            List numList = new List();
            while (count-- > 0)
            {
                commandLine = sr.ReadLine().ToCharArray();
                sr.ReadLine();
                keepGoing = true;
                isReverse = false;
                numList = sr.ReadLine().Trim('[', ']').Split(',').ToList();
                frontIndex = 0;
                backIndex = numList.Count - 1;

                for (int i = 0; i < commandLine.Length && keepGoing; i++)
                {   //RorD 커맨드 하나씩 실행 + error가 뜬 경우 keepGoing == false로 인해 실행 끝남
                    if (commandLine[i].Equals('R'))
                    {
                        if (!isReverse)
                            isReverse = true;
                        else
                            isReverse = false;
                    }
                    else
                    {   //D
                        if (frontIndex > backIndex || numList[0] == "")
                        {   //배열안에 아무것도 없음 or 처음 받은 값이 []인데 Delete 실행할때
                            sb.AppendLine("error");
                            keepGoing = false;
                            break;
                        }
                        if (isReverse)
                            backIndex--;
                        else
                            frontIndex++;
                    }   //인덱스만 수정
                }


                if (keepGoing)
                {   //출력과정
                    sb.Append("[");
                    if (isReverse)
                    {
                        for (int x = backIndex; x >= frontIndex; x--)
                            sb.Append(numList[x] + ",");
                    }
                    else
                    {
                        for (int x = frontIndex; x <= backIndex; x++)
                            sb.Append(numList[x] + ",");
                    }
                    if (sb[sb.Length - 1].Equals(','))
                        sb.Remove(sb.Length - 1, 1);
                    sb.AppendLine("]");

                }
            }
            Console.WriteLine(sb);

다른 사람 코드 보고 더 좋은 아이디어에 대해 찾아봐야 하는데 으음.. 이건 귀찮다


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함