FIXPAREN 올바르게 출력되는데 어쨰서 오답인지 모르겠습니다.

  • susul89
    susul89

    여러 케이스를 입력해봤는데 다올바르게 나오는데도
    오답이 뜨네요.. 무엇이 문제인지...

    알고리즘을 얼추 설명드리자면
    문자열을 입력을 받고(여러괄호들로 이루어진)
    제일 처음 괄호의 쌍을 찾고
    (그 괄호다음부터 검색을 하여 (0부터시작하여)
    왼쪽괄호면+1, 오른쪽괄호면-1 하여 -1면 해당의 올바른 쌍이다)

    그 쌍의 순서를 파악후 낮은 순서의 괄호로 바꾼다

    모든괄호쌍을 검사하고 출력

    이런식인데요... 올바르게 답이 나오는것 같은데 오답이 뜨네요..
    무엇이 문제인지

    • 아또한... 궁금한게 있는데 이것이 자료구조 문제라 스택같은 자료구조를 이용하여 푸는게 올바른 방법인것 같은데 제가 너무 하드코딩을 한것 같아서요... 이런식으로 푼게 비효울적인가요(올바르지 않나요?...)
    #include <iostream>
    #include<string>
    #include <string.h>
    #include <cstdlib>
    #include <stack>
    
    using namespace std;
    
    char Left[4] = { 0 };
    char Right[4] = { 0 };
    
    bool AnswerCheck(char A, char B);
    void MakeRight();
    void AdjustPath(int A, int B);
    string Input;
    int main()
    {
        int Count;
        cin >> Count;
        while (Count--)
        {
            cin >> Input >> Left[0] >> Left[1] >> Left[2] >> Left[3];
            MakeRight();
            for (int i = 0; i<Input.length(); i++)
            {
                int check = 0;
                if (Input[i] == Left[0] || Input[i] == Left[1] || Input[i] == Left[2] || Input[i] == Left[3])
                {
                    for (int j = i + 1; j < Input.length(); j++)
                    {
                        if (Input[j] == Right[0] || Input[j] == Right[1] || Input[j] == Right[2] || Input[j] == Right[3])
                        {
                            check--;
                            if (check == -1)
                            {
                                AdjustPath(i,j);
                            }
                        }
                        else
                        {
                            check++;
                        }
                    }
                }
            }
            cout << Input << endl;
        }
    }
    void MakeRight()
    {
        for (int i = 0; i < 4; i++)
        {
            if (Left[i] == '{')
                Right[i] = '}';
            if (Left[i] == '(')
                Right[i] = ')';
            if (Left[i] == '[')
                Right[i] = ']';
            if (Left[i] == '<')
                Right[i] = '>';
        }
    }
    
    void AdjustPath(int A, int B)
    {
        int LeftSequnce = 0;
        int RightSequnce = 0;
    
        for (LeftSequnce = 0; LeftSequnce < 4; LeftSequnce++)
        {
            if (Input[A] == Left[LeftSequnce])
                break;
        }
        for (RightSequnce = 0; RightSequnce < 4; RightSequnce++)
        {
            if (Input[B] == Right[RightSequnce])
                break;
        }
    
        if (LeftSequnce == RightSequnce)
        {
            return ;
        }
        else if (LeftSequnce < RightSequnce)
        {
            Input[B] = Right[LeftSequnce];
            return;
        }
        else if (LeftSequnce > RightSequnce)
        {
            Input[A] = Left[RightSequnce];
            return;
        }
    }
    

    8년 전
0개의 댓글이 있습니다.
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.