Notice
Recent Posts
Recent Comments
Today
Total
01-25 23:16
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[2020 KAKAO BLIND RECRUITMENT] ๊ด„ํ˜ธ ๋ณ€ํ™˜ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[2020 KAKAO BLIND RECRUITMENT] ๊ด„ํ˜ธ ๋ณ€ํ™˜

WonderJay 2022. 7. 8. 12:45
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/60058

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

๋‹ซํžŒ ๊ด„ํ˜ธ, ์—ด๋ฆฐ ๊ด„ํ˜ธ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด๋กœ ์žฌ๊ท€์ ์œผ๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค. ๋ฌธ์ œ์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ˆ˜๋„ ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„๋งŒ ํ•˜๋ฉด ๋œ๋‹ค!

 

 

[C++]

#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <iostream>

using namespace std;

bool isitbalanced(string p)
{
    int left= 0; int right = 0;
    for(auto & ele : p)
    {
        if(ele == '(') left ++;
        if(ele == ')') right ++;
    }
    return left == right ? true : false; // ๊ท ํ˜•์žกํžŒ ๋ฌธ์ž์—ด์ธ์ง€ ์•„๋‹Œ์ง€
}

bool isitcorrect(string p)
{
    stack<char> st;
    st.push(p[0]);
    for(int i = 1 ; i < p.size(); i ++){
        char ele = p[i];
        if(st.top() == '(' && ele == ')')
        {
            st.pop();
            continue;
        }
         if(st.top() == '(' && ele == '(')
        {
            st.push(ele);
            continue;
        }
         if(st.top() == ')' && ele == '(')
        {
            st.push(ele);
            continue;
        }
         if(st.top() == ')' && ele == ')')
        {
            st.push(ele);
            continue;
        } 
    }
    
    if(st.empty())
    {
      return true; // ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ด๋ฉด true
    }
    else
    {
      while(!st.empty()) st.pop(); // ์Šคํƒ์ด ๋นŒ๋•Œ๊นŒ์ง€ ํŒํŒ
      return false;
    }
}

string tfm(string p, string u = "", string v = ""){
    if(p.empty()) return p; // ์ž…๋ ฅ์ด ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 
    // to do :  p ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋งŒ๋“ค๊ธฐ
    
    for(int i = 0 ; i < p.size(); i ++) // ๋ฌธ์ž์—ด w๋ฅผ ๋‘ "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" u, v๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    {
        u.push_back(p[i]);
        if(isitbalanced(u))
        {
            v = p.substr(i+1);
            break;
        }
    }
    
    if(isitcorrect(u))
    {
        u += tfm(v);
        return u;
    }
    else
    {
      string temp = "("; // ๋นˆ ๋ฌธ์ž์—ด์— ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋กœ '('๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค. 
      temp += tfm(v); // ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค. 
      temp.push_back(')'); // ')'๋ฅผ ๋‹ค์‹œ ๋ถ™์ž…๋‹ˆ๋‹ค. 
      u.erase(u.begin()); u.erase(u.end()-1); //u์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ ,
        //๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด์„œ
      string temp_u = "";
      for(auto & ele : u)
      {
          if(ele == '(')
          {
              temp_u.push_back(')');
          }
          if(ele == ')')
          {
              temp_u.push_back('(');
          }
      }
        
        
      temp += temp_u;
      return temp;
    }
}

string solution(string p) {
    string answer = "";
    answer = tfm(p);
    return answer;
}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments