Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
                            Tags
                            
                        
                          
                          - command not found
- 언리얼 커스텀 플러그인
- hackerank
- 백준
- 실습
- UnrealMP
- 2단계로킹
- 데이터베이스 배움터
- Security
- Linux
- 백준 1253번
- OS
- oracle
- 민겸수
- 5639
- C++
- 오손데이터읽기
- Unreal
- FBX
- 1759번
- 비재귀셰그먼트
- 1967번
- objtofbx
- 언리얼 플러그인
- 의미와 무의미의 경계에서
- 셰그먼트트리
- 1253번
- UActor
- 트랜잭션 관리
- SQL
                            Archives
                            
                        
                          
                          - Today
- Total
fatalite
프로그래머스 - 괄호 변환 본문
주어진 내용을 그대로 구현하면 된다.
내가 구현하려고 했으면 시간이 오래 걸렸을 듯 하다.
관련된 내용은
- 스택(그러나, 나는 올바른 괄호 구하는데 스택을 사용하지 않았다.)
- 재귀(스택과 재귀!)
- string을 잘 다룰 수 있는가?
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool isRight(string s)
{
    int cnt = 0;
    for(char c : s)
    {
        if(c == '(')
        {
            cnt++;
        }
        else
        {
            if(cnt == 0)
            {
                return false;
            }
            cnt--;
        }
    }
    return true;
}
pair<string, string> GetBalanced(string s)
{
    int cntOpen = 0;
    int cntClose = 0;
    string u = "";
    string v = "";
    for(char c : s)
    {
        if(c == '(')
        {
            cntOpen++;
        }
        else
        {
            cntClose++;
        }
        u.push_back(c);
        if(cntClose == cntOpen)
        {
            break;
        }
    }
    
    return {u, s.substr(cntOpen + cntClose, 999)};
}
string Find(string s)
{
    if(s.empty()) return "";
    pair<string,string> uvPair = GetBalanced(s);
    string u = uvPair.first;
    string v = uvPair.second;
    if(isRight(u))
    {
        string answer = Find(v);
        u.append(answer);
        return u;
    }
    else
    {
        string answerU = "";
        answerU.push_back('(');
        string answerV = Find(v);
        answerU.append(answerV);
        answerU.push_back(')');
        cout << u << endl;
        for(int i = 1 ; i < u.length() -1 ; i++ )
        {
            if(u[i] == '(')
            {
                answerU.push_back(')');
            }
            else
            {
                answerU.push_back('(');
            }
        }
        return answerU;
    }
}
string solution(string p) {
    return Find(p);
}'코딩 인터뷰 > C++' 카테고리의 다른 글
| 프로그래머스 단어 변환 (1) | 2024.10.06 | 
|---|---|
| 프로그래머스 n진수 게임 (0) | 2024.10.01 | 
| 프로그래머스 C++ 삼각 달팽이 (0) | 2024.09.28 | 
| 구간 합 구하기 - 2042번 백준 (0) | 2023.10.01 | 
| 🏳 겹치는 건 싫어 - 20922번 백준 (2) | 2023.09.16 | 
