less than 1 minute read

코드

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define X first
#define Y second


int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n; cin >> n;
    int ans = 0;
    while (n--)
    {
        string s; cin >> s;
        stack<char> st;
        for (auto c : s)
        {
            if (st.empty() || st.top() != c)
            {
                st.push(c);
            }
            else if (st.top() == c)
            {
                st.pop();
            }
        }
        if (st.empty())
        {
            ans++;
        }
    }
    cout << ans;
}

간단한 스택 응용문제였습니다.
아치형 곡선을 그린다고 하니. 스택의 top과 지금 인덱스와 같다면 pop을 하여 스택이 비어있으면 좋은 단어 입니다.

10799 코드

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define X first
#define Y second


int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    string s; cin >> s;
    ll cnt = 0;
    ll ans = 0;
    int len = s.length();
    for (int i = 0; i < len; i++)
    {
        if (s[i] == '(')
        {
            cnt++;
        }
        else
        {
            if (s[i - 1] == '(')
            {
                cnt--;
                ans += cnt;
            }
            else
            {
                ans++;
                cnt--;
            }
        }
    }
    cout << ans;
    return 0;
}

이 문제같은 경우에는 cnt를 현재 맞출 수 있는 막대기의 갯수라고 생각을 하면 쉽습니다.

Leave a comment