CSP-S T3求调
  • 板块学术版
  • 楼主EastIsRed
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/22 07:11
  • 上次更新2023/11/2 12:02:53
查看原帖
CSP-S T3求调
437398
EastIsRed楼主2023/10/22 07:11
#include<cstdio>
#include<string>
#include<map>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
long long str[1024],mem_top;
int type_no[1024],tp_tot,var_tot;
char temp[15];
string nm[1024];
map<string,int>no,name;
struct ntype{
    long long siz,mx;
    vector<int>inc_tp;
    vector<long long>spos;
    vector<string>inc_name;
    ntype():siz(0),inc_tp(),inc_name(),spos(){}

}tps[105];
string find(const ntype& tp,long long addr)
{
    if(addr>=tp.siz)
        return "ERR";
    if(tp.inc_tp.size()==0)
        return "";
    int i=0;
    for(;i<tp.inc_tp.size()-1&&tp.spos[i+1]<=addr;i++);
    string res=find(tps[tp.inc_tp[i]],addr-tp.spos[i]);
    if(res!="ERR")
        res="."+tp.inc_name[i]+res;
    return res;
}
int main()
{
    //freopen("struct.in","r",stdin);
    //freopen("struct.out","w",stdout);
    scanf("%d",&n);
    no["byte"]=0;
    no["short"]=1;
    no["int"]=2;
    no["long"]=3;
    tps[0].siz=1;tps[0].mx=1;
    tps[1].siz=2;tps[1].mx=2;
    tps[2].siz=4;tps[2].mx=4;
    tps[3].siz=8;tps[3].mx=8;
    tp_tot=4;
    while(n--)
    {
        int op;
        scanf("%d",&op);
        if(op==1)
        {
            int siz;
            scanf("%s%d",temp,&siz);
            no[temp]=tp_tot;
            //long long mx=0;
            for(int i=0;i<siz;i++)
            {
                scanf("%s",temp);
                int nno=no[temp];
                tps[tp_tot].inc_tp.push_back(nno);
                if(tps[tp_tot].mx<tps[nno].mx)
                    tps[tp_tot].mx=tps[nno].mx;
                if(tps[tp_tot].siz%tps[nno].mx)
                    tps[tp_tot].siz=tps[tp_tot].siz+(tps[nno].mx-tps[tp_tot].siz%tps[nno].mx);
                tps[tp_tot].spos.push_back(tps[tp_tot].siz);
                tps[tp_tot].siz+=tps[tps[tp_tot].inc_tp[i]].siz;
                scanf("%s",temp);
                tps[tp_tot].inc_name.push_back(temp);
            }
            if(tps[tp_tot].siz%tps[tp_tot].mx)
                tps[tp_tot].siz=tps[tp_tot].siz+(tps[tp_tot].mx-tps[tp_tot].siz%tps[tp_tot].mx);
            printf("%lld %lld\n",tps[tp_tot].siz,tps[tp_tot].mx);
            tp_tot++;
        }
        else if(op==2)
        {
            scanf("%s",temp);
            int nno=no[temp];
            scanf("%s",temp);
            nm[var_tot]=temp;
            name[temp]=var_tot;
            type_no[var_tot]=nno;
            if(mem_top%tps[nno].mx)
                mem_top+=tps[nno].mx-mem_top%tps[nno].mx;
            str[var_tot]=mem_top;
            printf("%lld\n",mem_top);
            mem_top+=tps[nno].siz;
            var_tot++;
        }
        else if(op==3)
        {
            string s;
            cin>>s;
            int lst=0,nno;
            long long nmem=0;
            for(int i=0;i<s.size();i++)
                if(s[i]=='.')
                {
                    if(lst==0)
                    {
                        nno=name[s.substr(lst,i)];
                        nmem=str[nno];
                        nno=type_no[nno];
                        //printf("the type of var %s is %d\n",s.substr(lst,i).c_str(),nno);
                    }
                    else
                    {
                        int j=0;
                        string temp2=s.substr(lst,i-lst);
                        for(;temp2!=tps[nno].inc_name[j];j++);
                        nmem+=tps[nno].spos[j];
                        nno=tps[nno].inc_tp[j];
                        //printf("the type of var %s is %d\n",s.substr(lst,i-lst).c_str(),nno);
                    }
                    lst=i+1;
                }
            if(lst==0)
                nmem=str[name[s]];
            else
            {
                int i=0;
                string temp2=s.substr(lst,s.size()-lst);
                for(;temp2!=tps[nno].inc_name[i];i++);
                nmem+=tps[nno].spos[i];
                //printf("the type of var %s is %d\n",s.substr(lst,s.size()-lst).c_str(),tps[nno].inc_tp[i]);
            }
            printf("%lld\n",nmem);
        }
        else
        {
            long long addr;
            scanf("%lld",&addr);
            int nno=lower_bound(str,str+var_tot,addr)-str;
            nno--;//printf("%d %lld ",nno,str[nno]);
            if(nno<0)
            {
                printf("ERR\n");
                continue;
            }
            string res=find(tps[type_no[nno]],addr-str[nno]);
            if(res!="ERR")
                res=nm[nno]+res;
            printf("%s\n",res.c_str());
        }
    }
    return 0;
}

云斗上85pts,洛谷上75pts,错的集中于1,2,3号测试点,看不出来哪里错了,求调

2023/10/22 07:11
加载中...