悬赏3RMB求条

P2830 写程序

houpingze @ 2024-08-03 14:25:33

如题,wa&tle 16pts

#include<bits/stdc++.h>

using namespace std;
struct node{
    int sz;
    int z[114];
};
map<string,node>a;
//int a[11451][111];
string x,y;
int n,m;
int zhi;
void dfscout(){
    int num=0,flg=0;
        string blm="";
    for(int i=y.size() -1;i>=0;i--){
        if(y[i]==']'||y[i]=='[') continue; 
        int j=i;
        for(;i>=0;i--){
            if(y[i]<'0'||y[i]>'9') break; 
        }
        blm="";
        if(flg==0)
            for(int w=i+1;w<=j;w++) num=num*10+y[w]-'0';
        flg=1;

        if(y[i]==']'||y[i]=='[') continue;
        for(;i>=0;i--){
            if(y[i]=='[') break;
            string tmp="";
            tmp+=y[i];
            blm=tmp+blm;
        }
//      cout<<"NOW:"<<blm<<' '<<num<<endl;
        if(a[blm].sz<=num){
            cout<<-1;
            exit(0);
        }
        num=a[blm].z[num];

    }
    cout<<num<<'\n';
}

void fz(){ 
    int num=0,flg=0;
    string blm="";
    for(int i=x.size()-1;i>=0;i--){
        if(x[i]==']'||x[i]=='[') continue;
        int j=i;
        for(;i>=0;i--){
            if(x[i]<'0'||x[i]>'9') break; 
        }

        if(flg==0){
            num=0;
            for(int w=i+1;w<=j;w++) num=num*10+x[w]-'0';
        }
        flg=1;
        blm="";
        if(x[i]==']'||x[i]=='[') continue;
        for(;i>=0;i--){
            if(x[i]=='[') break;
            string tmp="";
            tmp=x[i];
            blm=tmp+blm;
        }

//      cout<<"NOW:"<<blm<<'|'<<num<<endl;
        if(a[blm].sz<=num){
            cout<<-1;
            exit(0);
        }

        num=a[blm].z[num];
    }
    a[blm].z[num]=zhi;
}
int main(){
    while(1){
        cin>>x;
        n=x.size(); 
        if(x=="int"){
            string blm="";
            char c;
            while(1){
                cin>>c;
                if(c=='['){
                    break;
                }
                blm+=c;
            }
            int sz=0;
            while(1){
                cin>>c;
                if(c==']') {
                    break;
                }
                sz=sz*10+c-'0';
            }
            a[blm].sz=sz;

        }else if(x=="cout"){
            cin>>y;
            dfscout();
        }else{
            cin>>zhi;
            fz();
        }
    } 

    return 0;
}

|