萌新求助!!!!!!!!!83pts~
  • 板块P2830 写程序
  • 楼主STUDENT00
  • 当前回复7
  • 已保存回复7
  • 发布时间2022/8/24 09:59
  • 上次更新2023/10/27 13:55:23
查看原帖
萌新求助!!!!!!!!!83pts~
658786
STUDENT00楼主2022/8/24 09:59

代码很好理解:

#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;
}
2022/8/24 09:59
加载中...