萌新求助!!!!!!!!!83pts~

P2830 写程序

STUDENT00 @ 2022-08-24 09:59:24

代码很好理解:


#include<bits/stdc++.h>
using namespace std;
string str;
map<string,int> f;
struct node{
    int len,a[110]={0};
};
vector<node> v; 
int k;
int read(int k){
    stack<int> q;
    int t=0,i=k;
    do{
        if(str[i]>='0'&&str[i]<='9'){
            while(str[i]>='0'&&str[i]<='9'){
                t=t*10+str[i]-'0';
                i++;
            }
            i--;
        }else if(str[i]!='['&&str[i]!=']'){
            string p="";
            while(str[i]!='['){
                p+=str[i];
                i++;
            }
            q.push(f[p]);
        }else if(str[i]==']'){
            if(v[q.top()].len<=t) return -1; 
            t=v[q.top()].a[t];
            q.pop();
        }
        i++;
    }while(!q.empty());
    return t;
}
int main(){
    node q;
    v.push_back(q);
    while(getline(cin,str)){
        if(str[0]=='i'&&str[1]=='n'&&str[2]=='t'&&str[3]==' '){
            string p="";
            int i;
            for(i=4;str[i]!='[';i++) p+=str[i];
            int num=read(i+1);
            k++;
            f[p]=k;
            node q;
            q.len=num;
            v.push_back(q);
        }else if(str[0]=='c'&&str[1]=='o'&&str[2]=='u'&&str[3]=='t'&&str[4]==' '){
            int num=read(5);
            if(num==-1){
                printf("-1");
                return 0;
            }else printf("%d\n",num);
        }else{
            string p="";
            int i;
            for(i=0;str[i]!='[';i++) p+=str[i];
            int num=read(i+1);
            if(num==-1||v[f[p]].len<=num){
                printf("-1");
                return 0;
            }
            v[f[p]].a[num]=read(str.find(" ")+1);
        }
    }
    return 0;
}

by STUDENT00 @ 2022-08-24 10:02:49

现在91pts,求大佬!!!


by STUDENT00 @ 2022-08-24 10:03:02


#include<bits/stdc++.h>
using namespace std;
string str;
map<string,int> f;
struct node{
    int len,a[110]={0};
};
vector<node> v; 
int k;
int read(int k){
    stack<int> q;
    int t=0,i=k;
    do{
        if(str[i]>='0'&&str[i]<='9'){
            while(str[i]>='0'&&str[i]<='9'){
                t=t*10+str[i]-'0';
                i++;
            }
            i--;
        }else if(str[i]!='['&&str[i]!=']'){
            string p="";
            while(str[i]!='['){
                p+=str[i];
                i++;
            }
            q.push(f[p]);
        }else if(str[i]==']'){
            if(v[q.top()].len<=t) return -1; 
            t=v[q.top()].a[t];
            q.pop();
        }
        i++;
    }while(!q.empty());
    return t;
}
int main(){
    node q;
    v.push_back(q);
    while(getline(cin,str)){
        if(str[0]=='i'&&str[1]=='n'&&str[2]=='t'&&str[3]==' '){
            string p="";
            int i;
            for(i=4;str[i]!='[';i++) p+=str[i];
            int num=read(i+1);
            if(num==-1){
                printf("-1");
                return 0;
            }
            k++;
            f[p]=k;
            node q;
            q.len=num;
            v.push_back(q);
        }else if(str[0]=='c'&&str[1]=='o'&&str[2]=='u'&&str[3]=='t'&&str[4]==' '){
            int num=read(5);
            if(num==-1){
                printf("-1");
                return 0;
            }else printf("%d\n",num);
        }else{
            string p="";
            int i;
            for(i=0;str[i]!='[';i++) p+=str[i];
            int num=read(i+1);
            if(num==-1||v[f[p]].len<=num){
                printf("-1");
                return 0;
            }
            v[f[p]].a[num]=read(str.find(" ")+1);
        }
    }
    return 0;
}

by STUDENT00 @ 2022-08-24 10:06:49

已AC,代码如下:


#include<bits/stdc++.h>
using namespace std;
string str;
map<string,int> f;
struct node{
    int len,a[110]={0};
};
vector<node> v; 
int k;
bool flag;
int read(int k){
    stack<int> q;
    int t=0,i=k;
    do{
        if(str[i]>='0'&&str[i]<='9'){
            while(str[i]>='0'&&str[i]<='9'){
                t=t*10+str[i]-'0';
                i++;
            }
            i--;
        }else if(str[i]!='['&&str[i]!=']'){
            string p="";
            while(str[i]!='['){
                p+=str[i];
                i++;
            }
            q.push(f[p]);
        }else if(str[i]==']'){
            if(v[q.top()].len<=t) return -1; 
            t=v[q.top()].a[t];
            q.pop();
        }
        i++;
    }while(!q.empty());
    return t;
}
int main(){
    node q;
    v.push_back(q);
    while(getline(cin,str)){
        if(str[0]=='i'&&str[1]=='n'&&str[2]=='t'&&str[3]==' '){
            string p="";
            int i;
            for(i=4;str[i]!='[';i++) p+=str[i];
            int num=read(i+1);
            k++;
            f[p]=k;
            node q;
            q.len=num;
            v.push_back(q);
        }else if(str[0]=='c'&&str[1]=='o'&&str[2]=='u'&&str[3]=='t'&&str[4]==' '){
            int num=read(5);
            if(num==-1){
                printf("-1"),flag=1;
                return 0;
            }else printf("%d\n",num),flag=1;
        }else{
            string p="";
            int i;
            for(i=0;str[i]!='[';i++) p+=str[i];
            int num=read(i+1);
            if(num==-1||v[f[p]].len<=num){
                printf("-1"),flag=1;
                return 0;
            }
            v[f[p]].a[num]=read(str.find(" ")+1);
        }
    }
    if(!flag) printf("-1"); 
    return 0;
}

by fengziyi @ 2022-08-24 10:12:37

@YuRuochen AC了就去写题解吧 别发出来


by fengziyi @ 2022-08-24 10:17:14

@YuRuochen 会不会出现诸如

int array[array[1]]
cout array[array[1]]

的情况a(求教


by STUDENT00 @ 2022-08-24 10:36:00

对,我就是这个没考虑到,加了个flag就AC了。


by fengziyi @ 2022-08-24 10:54:58

@YuRuochen thx


|