結果

問題 No.708 (+ー)の式
コンテスト
ユーザー vjudge1
提出日時 2025-11-16 11:07:07
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,956 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 169,888 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-11-16 11:07:10
合計ジャッジ時間 2,722 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
string p;
stack<char> s;  
stack<char> op; 


stack<int> num; 

stack<int> tmp; 


inline int id(char ch)
{ 
    if (ch == '+' || ch == '-')
        return 1;
    if (ch == '*' || ch == '/')
        return 2;
    if (ch == '^')
        return 3;
    if (ch == '(' || ch == ')')
        return 0;
    return -1;
}
inline string solve(string p)
{ 
    while (s.size())
        s.pop();
    while (op.size())
        op.pop();
    for (int i = 0; i < int(p.size()); i++)
    {
        if (p[i] >= '0' && p[i] <= '9')
            s.push(p[i]);
        else
        {
            if (p[i] == '(')
            {
                op.push(p[i]);
                continue;
            }
            if (p[i] == ')')
            {
                while (!op.empty() && op.top() != '(')
                {
                    s.push(op.top());
                    op.pop();
                }
                if (!op.empty())
                    op.pop();
                continue;
            }
            if (id(p[i]) <= 2)
            {
                while (!op.empty() && id(op.top()) >= id(p[i]))
                {
                    s.push(op.top());
                    op.pop();
                }
            }
            else
            {
                while (!op.empty() && id(op.top()) > id(p[i]))
                {
                    s.push(op.top());
                    op.pop();
                }
            }
            op.push(p[i]);
        }
    }
    while (!op.empty())
    {
        s.push(op.top());
        op.pop();
    }
    string res = "";
    while (!s.empty())
    {
        res += s.top();
        s.pop();
    }
    reverse(res.begin(), res.end()); 
    return res;
}
inline int f(int x, int y, char ch)
{
    if (ch == '+')
        return x + y;
    if (ch == '-')
        return x - y;
    if (ch == '*')
        return x * y;
    if (ch == '/')
        return x / y;
    if (ch == '^')
        return int(pow(x, y));
    return -1;
}
inline void Query(string s)
{
    while (num.size())
        num.pop();
    for (int i = 0; i < int(s.size()); i++)
    {
        if (s[i] >= '0' && s[i] <= '9')
            num.push(s[i] - '0');
        else
        {
            while (tmp.size())
                tmp.pop();
            while (num.size())
            {
                tmp.push(num.top());
                num.pop();
            }
            while (tmp.size())
            {
                num.push(tmp.top());
                // cout << tmp.top() << " ";
                tmp.pop();
            }
            // for(int j=i;j<int(s.size());j++)cout<<s[j]<<" ";
            // cout<<"\n";
            int a = num.top();
            num.pop();
            int b = num.top();
            num.pop();
            num.push(f(b, a, s[i]));
        }
    }
    cout << num.top() << "\n";
}
signed main()
{


    cin >> p;
    Query(solve(p));
}
0