107979鉄棒(C++)



#include <iostream>
#include<stack>
#include<string>
using namespace std;

int main() {
    string command;
    string pre_com ="push";
    stack<char> mine;
    cin >> command;
    int numOfBar = 0;
    for (int j = 0; j < command.length(); j++) {
        if (command[j] == '(') {
            mine.push(command[j]);
            pre_com = "push";
        }
        else {
            if (pre_com == "push") {
                mine.pop();
                pre_com = "pop";
                numOfBar += mine.size();
            }
            else if (pre_com == "pop") {
                mine.pop();
                pre_com = "pop";
                numOfBar++;
            }
        }
    }
    cout << numOfBar;
}
スタックの説明を参照してください.
https://velog.io/@kimeunseo58/%EC%8A%A4%ED%83%9D
スタックソース:
https://velog.io/@kimeunseo58/10828%EC%8A%A4%ED%83%9DC