65pts #14-#20 玄关
查看原帖
65pts #14-#20 玄关
504403
VDLevUp楼主2024/10/9 20:12
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<cmath>
using namespace std;
struct type{
	string name;
	int size,need;
	vector<type*>ch;
	vector<string>tn;
	type():need(1),size(0),ch({}),tn({}),name(""){}
};
struct block{
	int l,r;
	type* t;
	block():l(0),r(0),t(nullptr){}
};
map<string,type*>ntt;
map<type*,string>ttn;
map<block*,string>btn;
map<string,block*>ntb;
vector<block*>vars;
type* add_type(string na,int count){
	type* cur=new type();
	type* oth;
	cur->name=na;
	string opt,var;
	int st=0;
	while(count--) {
		cin>>opt>>var;
		oth=ntt[opt];
		cur->ch.push_back(oth);
		cur->tn.push_back(var);
		cur->need=max(cur->need,oth->need);
		st=ceil(1.0*st/oth->need)*oth->need;
		st+=oth->size;
	}
	cur->size=ceil(1.0*st/cur->need)*cur->need;
	ntt[na]=cur;
	ttn[cur]=na;
	return cur;
}
block* add_var(string tna,string na){
	block* cur=new block();
	cur->l=0;
	if(!vars.empty()) cur->l=vars.back()->r+1;
	cur->t=ntt[tna];
	cur->l=ceil(1.0*cur->l/cur->t->need)*cur->t->need;
	cur->r=cur->l+cur->t->size-1;
	vars.push_back(cur);
	btn[cur]=na;
	ntb[na]=cur;
	return cur;
}
void split(string cur,vector<string>&tar){
	string p="";
	for(char i : cur){
		if(i=='.') tar.push_back(p),p="";
		else p+=i;
	}
	if(p!="") tar.push_back(p);
}
int addr(string vari){
	vector<string>lis;
	split(vari,lis);
	block* cur=ntb[lis[0]];
	int l=cur->l;
	type* nxt=cur->t;
	//cout<<cur->t->name<<'\n';
	for(int i=1,st;i<lis.size();++i) {
		//cout<<"It's "<<i<<'\n';
		st=0;
		for(int j=0;j<nxt->tn.size();++j) {
			if(nxt->tn[j]==lis[i]) {//cout<<"Now is "<<nxt->tn[j]<<'\n';
				nxt=nxt->ch[j];
				break;
			}
			st=ceil(1.0*st/nxt->ch[j]->need)*nxt->ch[j]->need;
			st+=nxt->ch[j]->size;
		}
		st=ceil(1.0*st/nxt->need)*nxt->need;
		l+=st;
	}
	return l;
}
void access(int pos){
	block* cur=nullptr;
	for(block* i : vars) {
		if(i->l<=pos&&i->r>=pos) {
			cur=i;
			break;
		}
	}
	if(!cur) {
		cout<<"ERR\n";
		return;
	}
	pos-=cur->l;
	type* nxt=cur->t;
	string ans=btn[cur];
	while(true){
		if(nxt->tn.size()==0) {
			cout<<ans<<'\n';
			return;
		}
		ans+='.';
		int ed=0,i=0;
		while(i<nxt->tn.size()) {
			ed+=nxt->ch[i]->size;
			if(pos<ed) {
				ans+=nxt->tn[i];
				nxt=nxt->ch[i];
				break;
			}
			if(i==nxt->tn.size()-1) {
				cout<<"ERR\n";
				return;
			}
			ed=ceil(1.0*ed/nxt->ch[i+1]->need)*nxt->ch[i+1]->need;
			if(pos<ed) {
				cout<<"ERR\n";
				return;
			}
			i++;
		}
		//cout<<ans<<'\n';
	}
}
int main(){
	type *Long,*Int,*Short,*Byte;
	Long=add_type("long",0);
	Int=add_type("int",0);
	Short=add_type("short",0);
	Byte=add_type("byte",0);
	Long->need=Long->size=8;
	Int->need=Int->size=4;
	Short->need=Short->size=2;
	Byte->need=Byte->size=1;
	int n,op;
	string tmpa,tmpb;
	cin>>n;
	while(n--){
		cin>>op;
		if(op==1) {
			cin>>tmpa>>op;
			type* cur=add_type(tmpa,op);
			cout<<cur->size<<' '<<cur->need<<'\n';
		}
		else if(op==2){
			cin>>tmpa>>tmpb;
			cout<<add_var(tmpa,tmpb)->l<<'\n';
		}
		else if(op==3) {
			cin>>tmpa;
			cout<<addr(tmpa)<<'\n';
		}
		else if(op==4) {
			cin>>op;
			access(op);
		}
	}
	return 0;
}
2024/10/9 20:12
加载中...