【求助】67pts,细节未知错误,带详细情况
  • 板块P2830 写程序
  • 楼主AIH_NUI233
  • 当前回复10
  • 已保存回复11
  • 发布时间2023/10/28 12:00
  • 上次更新2023/11/2 11:06:34
查看原帖
【求助】67pts,细节未知错误,带详细情况
1054398
AIH_NUI233楼主2023/10/28 12:00

做法清奇qwqqq

  • 注释打了

  • 嵌套所有地方都已经考虑

  • 使用奇怪的map,不知此处是否会导致【除代码自生造成外的】TLE

  • #5 #9 TLE;

  • #6 WA:wrong answer On line 1 column 1, read -, expected 0;

  • #7 WA:wrong answer On line 2 column 1, read 3, expected 1.

#include <bits/stdc++.h>
#define re register
using namespace std;
int p,cnt=0;
map<string,int> to;//名字->int
map<pair<int,int>,int> w; //名-位置-值
map<string,int> sz;//名-大小
string s;

inline int getpos(string t){//寻找实际地址
    if(t[0]>='0'&&t[0]<='9'){
        int pos=0;
        for(re int i=0;i<t.size()-1;i++)
            pos=pos*10+t[i]-'0';
        return pos;
    }
    else if(t[0]=='-'){
        return -1;
    }
    else {//此结构(St~Ed)在代码中多次出现,由于个人原因未做整理
        string pos="";//St
        string name="";
        bool f=false;
        for(re int i=0;i<t.size()-1;i++){
            if(t[i]=='['&&(!f)){
                f=true;
                continue;
            }
            if(f) pos=pos+t[i];//下标递归用
            else name=name+t[i];//数组名
        }
        int poss=getpos(pos);//开始向[]内寻找
        if(poss<0||poss>=sz[name]) return -1;//Ed,下标 <0 或 >数组范围 时返回-1
        else return (w[make_pair(to[name],poss)]);
    }
}

int main(){
    while(!cin.eof()){
        cin>>s;
        if(s=="int") {//定义
            string nas;//数组
            cin>>nas;
            string name="";//St
            string size="";
            bool f=false;
            for(int i=0;i<nas.size()-1;i++){
                if(nas[i]=='['&&(!f)){
                    f=true;
                    continue;
                }
                if(f) size=size+nas[i];
                else name=name+nas[i];
            }
            int sizz=getpos(size);//实际下标
            if(sizz<=0){
                cout<<-1<<endl;
                return 0;
            }//Ed
            else{
                to[name]=cnt++;//把数组名转化为cnt
                for(int i=0;i<sizz;i++) w[make_pair(cnt,i)]=0;//初始化
                sz[name]=sizz;//数组大小
            }

            // cout<<size<<" "<<name<<" "<<sz[name]<<endl;
        }

        else if(s=="cout") {//输出
            string t;//地址
            cin>>t;
            bool f=false;
            string pos="";//St
            string name="";
            for(int i=0;i<t.size()-1;i++){
                if(t[i]=='['&&(!f)){
                    f=true;
                    continue;
                }
                if(f) pos=pos+t[i];
                else name=name+t[i];
            }
            int poss=getpos(pos);
            if(poss<0||poss>=sz[name]){
                cout<<-1<<endl;
                return 0;
            }//Ed
            else cout<<w[make_pair(to[name],poss)]<<endl;//见定义时理解
        }
        else{
            int inf;
            cin>>inf;
            bool f=false;
            string pos="";//St
            string name="";
            for(int i=0;i<s.size()-1;i++){
                if(s[i]=='['&&(!f)){
                    f=true;
                    continue;
                }
                if(f) pos=pos+s[i];
                else name=name+s[i];
            }
            int poss=getpos(pos);
            if(poss<0||poss>=sz[name]){
                cout<<-1<<endl;
                return 0;
            }//Ed
            else w[make_pair(to[name],poss)]=inf;
        }
    }
    return 0;
}

求调,不胜感激qwqqq

2023/10/28 12:00
加载中...