ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 쇠막대기
    백준알고리즘 2019. 9. 8. 15:18

    #include <iostream>
    #include <string>
    #include <stack>
    #include <vector>
    using namespace std;
    
    int main(void)
    {
    	int nstick = 0;	//총 막대기
    	int result = 0;
    
    	string str;
    	stack<char> stk;
    
    	cin >> str;
    
    	vector<int> lstick;	//1개의 레이저가 통과하는 막대기
    
    	for (int i = 0; i < str.length(); i++)
    	{
    		if (str[i] == '(')
    			stk.push(str[i]);
    		else
    		{
    			if (str[i - 1] == str[i])
    			{
    				stk.pop();
    				nstick++;
    			}
    			else
    			{
    				stk.pop();
    				lstick.push_back(s.size());
    			}	//스택에 남아있는 '('수 = 레이저가 통과하는 막대기 수			
    		}
    	}
    
    	for (int i = 0; i < (int)lstick.size(); i++)
    	{
    		result += lstick[i];
    	}
    	cout << result + nstick << '\n';
    	return 0;
    }

    쇠막대기문제

    처음에 했을땐 엄청어려웠던걸로 기억하는데..

    스택이든 큐든 사실 필요없이도 풀수있는문제지만, 스택써서하는풀이

    '백준알고리즘' 카테고리의 다른 글

    미네랄  (0) 2019.09.10
    효율적인 해킹  (0) 2019.09.09
    상근이의 여행  (0) 2019.09.08
    오큰수  (0) 2019.09.08
    스타트와 링크  (0) 2019.09.05

    댓글

Designed by Tistory.