25分求调玄关
查看原帖
25分求调玄关
777809
IOI_AK_TLR楼主2023/10/22 16:50
#include <bits/stdc++.h>
using namespace std;
string s,t,s_name,s_maxn,s_loc;
int s_int_loc,val;
struct Node{
	int maxn;
	int dat[105];
};
map<string,Node>mp;
bool flag=false;
inline bool pd(string str)
{
	for(int i=0;i<(int)str.size();i++)
		if(str[i]<'0'||str[i]>'9')
			return false;
	return true;
}
int f(string x)
{
	if(pd(x))
		return stoi(x);
	string x_name,x_loc;
	int x_int_loc;
	x_name=x.substr(0,x.find('['));
	x_loc=x.substr(x.find('[')+1);
	x_loc=x_loc.substr(0,x_loc.size()-1);
	x_int_loc=f(x_loc);
	if(flag)
		return 101;
	if(x_int_loc>=mp[x_name].maxn)
	{
		flag=true;
		return 101;
	}
	return mp[x_name].dat[x_int_loc];
}
string qkg(string str)
{
	int len=str.size(),start=0;
	while(str[len-1]==' ')
		len--;
	while(str[start]==' ')
		start++,len--;
	return str.substr(start,len);
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	while(getline(cin,s))
	{
		s=qkg(s);
		if(s.substr(0,4)=="int ")
		{
			t=s.substr(s.rfind(' ')+1);
			s_name=t.substr(0,t.find('['));
			s_maxn=t.substr(t.find('[')+1);
			s_maxn=s_maxn.substr(0,s_maxn.size()-1);
			mp[s_name].maxn=f(s_maxn);
			if(flag)
			{
				cout<<-1;
				return 0;
			}
		}
		else if(s.substr(0,5)=="cout ")
		{
			t=s.substr(s.rfind(' ')+1);
			s_name=t.substr(0,t.find('['));
			s_loc=t.substr(t.find('[')+1);
			s_loc=s_loc.substr(0,s_loc.size()-1);
			s_int_loc=f(s_loc);
			if(flag||s_int_loc>=mp[s_name].maxn)
			{
				cout<<-1;
				return 0;
			}
			cout<<mp[s_name].dat[s_int_loc]<<'\n';
		}
		else
		{
			t=s.substr(0,s.find(' '));
			s_name=t.substr(0,t.find('['));
			s_loc=t.substr(t.find('[')+1);
			s_loc=s_loc.substr(0,s_loc.size()-1);
			s_int_loc=f(s_loc);
			if(flag||s_int_loc>=mp[s_name].maxn)
			{
				cout<<-1;
				return 0;
			}
			val=stoi(s.substr(s.rfind(' ')+1));
			mp[s_name].dat[s_int_loc]=val;
		}
	}
	return 0;
}


2023/10/22 16:50
加载中...