四十分WA求助/(ㄒoㄒ)/~~
查看原帖
四十分WA求助/(ㄒoㄒ)/~~
818995
CYJ331楼主2025/7/26 20:17
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1001;
ll n,a[N],b[N],c[N],l1[N],l2[N],l3[N];
string s;
inline ll POW(ll a,ll b){
    ll res=1;
    while(b){
        if(b%2==1)res*=a;
        a=a*a,b/=2;
    }
    return res;
}
inline ll dfs(ll l,ll r){
    if(l1[r]>=l)
        if(s[l1[r]]=='+')
            return dfs(l,l1[r]-1)+dfs(l1[r]+1,r);
        else 
            return dfs(l,l1[r]-1)-dfs(l1[r]+1,r);
    else if(l2[r]>=l)
        if(s[l2[r]]=='*')
            return dfs(l,l2[r]-1)*dfs(l2[r]+1,r);
        else 
            return dfs(l,l2[r]-1)/dfs(l2[r]+1,r);
    else if(l3[r]>=l)
        return POW(dfs(l,l3[r]-1),dfs(l3[r]+1,r));
    else 
        if(s[l]=='('&&s[r]==')')
            return dfs(l+1,r-1);
        else {
            ll x=0;
            for(ll i=l;i<=r;i++)
                x*=10,x+=s[i]-'0';
            return x;
        }
}
int main(){
    cin>>s;
    s=' '+s;
    n=s.size()-1;
    ll x=0;
    for(ll i=1;i<=n;i++){
        if(s[i]=='(')x++;
        if(s[i]==')')x--;
        if(s[i]=='+')a[x]=i;
        if(s[i]=='-'){
            if(i==1||(s[i-1]<'0'||s[i-1]>'9'))
                s.insert(i,1,'0'),n++;
            else a[x]=i;
        }
        if(s[i]=='*'||s[i]=='/')b[x]=i;
        if(s[i]=='^')c[x]=i;
        l1[i]=a[x],l2[i]=b[x],l3[i]=c[x];
    }
    cout<<dfs(1,n)<<endl;
    return 0;
}
2025/7/26 20:17
加载中...