Korloa 2022-01-31 09:07 采纳率: 66.7%
浏览 50
已结题

请看一下我的代码哪里错了

CSP-S 2020 表达式


#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
using namespace std;
struct node{
    char op;
    int value;
    int dp[2];
    vector<node*>son;
    node(){
        dp[0]=0;
        dp[1]=0;
        value=0;
        op='F';
    }
};
stack<node*>fuel;                    
node* date[100000];
char str[100000];
int tot=0,quenum,que;
using namespace std;
int Dfs_solve(node* x){
    if(x->op=='F'){
        return 0;
    }
    for(int i=0;i<x->son.size();i++){
        Dfs_solve(x->son[i]);
    }
    if(x->op=='!'){
        x->value=(!x->son[0]->value);
    }
    else if(x->op=='&'){
        x->value=(x->son[0]->value & x->son[1]->value); 
    }
    else if(x->op=='|'){
        x->value=(x->son[0]->value | x->son[1]->value); 
    }
    return 0;
}
void Dfs_dp(node* x,node* father){
    if(father==NULL){
        x->dp[0]=0;
        x->dp[1]=1;
    }
    else{
        if(father->op=='!'){
            x->dp[0]=father->dp[1];
            x->dp[1]=father->dp[0]; 
        }
        else if(father->op=='&' || father->op=='|'){
            node *other;
            if(father->son[0]==x){
                other=father->son[1];
            }
            else{
                other=father->son[0];
            }
            
            if(father->op=='&'){
                x->dp[0]=father->dp[0];
                x->dp[1]=father->dp[other->value]; 
            }
            else{
                x->dp[1]=father->dp[1];
                x->dp[0]=father->dp[other->value];
            }
        }
    }
    for(int i=0;i<x->son.size();i++){
        Dfs_dp(x->son[i],x);
    }
    return;
}
int main(){
    while(scanf("%s",&str)){
        if(str[0]=='x'){
            int num=0;
            for(int i=strlen(str)-1;i>0;i--){
                num=(num<<3)+(num<<1)+(str[i]^48);
            }
            date[num]=new node;
            fuel.push(date[num]);
        }
        else if(str[0]=='&' || str[0]=='|'){
            node* a;
            node* b;
            node* c=new node;
            a=fuel.top();
            fuel.pop();
            b=fuel.top();
            fuel.pop();
            c->op=str[0];
            c->son.push_back(a);
            c->son.push_back(b);
            fuel.push(c);
        }
        else if(str[0]=='!'){
            node* a;
            node* b=new node;
            a=fuel.top();
            fuel.pop();
            b->son.push_back(a);
            b->op='!';
            fuel.push(b);
        }
        else{
            for(int i=strlen(str)-1;i>=0;i--){
                tot=(tot<<3)+(tot<<1)+(str[i]^48);
            }
            for(int i=1;i<=tot;i++){
                scanf("%d",&date[i]->value);
            }
            break;
        }
    }
    node* root=fuel.top();
    Dfs_solve(root);
    Dfs_dp(root,NULL);
    scanf("%d",&quenum);
    for(int i=0;i<quenum;i++){
        scanf("%d",&que);
        printf("%d\n",date[que]->dp[!date[que]->value]);
    }
    return 0;
}

不知道为什么洛谷提交一直出错
Runing Error
请大家帮一忙,我快崩溃了

  • 写回答

3条回答 默认 最新

  • 关注

    第77行,while(scanf("%s",&str))这里,改成 while(scanf("%s",str)),str前面的&符号删掉,str本身就是数组地址,不需要再用&

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月9日
  • 已采纳回答 2月1日
  • 创建了问题 1月31日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line