codeforces87B模拟

Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language’s description below carefully and follow it and not the similar rules in real programming languages.

There is a very powerful system of pointers on &K* — you can add an asterisk to the right of the existing type X — that will result in new type X * . That is called pointer-definition operation. Also, there is the operation that does the opposite — to any type of X, which is a pointer, you can add an ampersand — that will result in a type &X, to which refers X. That is called a dereference operation.

The &K* language has only two basic data types — void and errtype. Also, the language has operators typedef and typeof.

The operator “typedef A B” defines a new data type B, which is equivalent to A. A can have asterisks and ampersands, and B cannot have them. For example, the operator typedef void** ptptvoid will create a new type ptptvoid, that can be used as void**.
The operator “typeof A” returns type of A, brought to void, that is, returns the type void**…, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**.
An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype
 =  &errtype  =  errtype. An attempt to use the data type that hasn’t been defined before that will also lead to the errtype.

Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change.

Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &T *  is always equal to T.

Note, that the operators are executed consecutively one by one. If we have two operators “typedef &void a” and “typedef a* b”, then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &void* = void (see sample 2).

Vasya does not yet fully understand this powerful technology, that’s why he asked you to help him. Write a program that analyzes these operators.

Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of operators. Then follow n lines with operators. Each operator is of one of two types: either “typedef A B”, or “typeof A”. In the first case the B type differs from void and errtype types, and besides, doesn’t have any asterisks and ampersands.

All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10.

Output
For every typeof operator print on the single line the answer to that operator — the type that the given operator returned.

Examples
Input
5
typedef void* ptv
typeof ptv
typedef &&ptv node
typeof node
typeof &ptv
Output
void*
errtype
void

typedef A B 中A是一个字符串,B是A的指针,字符串有两种类型 ,取决于字符串中‘*’和‘&’的个数,&个数多那么字符串类型为errtype,如果&数量一样或者少那么类型为void,typeof A是查询指针A指向的字符串的类型,如果是errtype直接输出errtype,如果之前没有定义过也输出errtype,如果是void则要输出字符串本身,但是要去除所有&,相应的去除相同个数的星号。

#include<map>
#include<algorithm>
#include<string>
using namespace std;
map<string,int>q;
int n;
int main ()
{
	cin>>n;
	q["void"]=1;
	for(int i=0;i<n;i++)
	{
		string take;
		cin>>take;
		if(take=="typedef")
		{
			string s1,s2;
			cin>>s1>>s2;
			string str="";
			int cnt=0;
			for(int i=0;i<s1.size();i++)
			{
				if(s1[i]=='&')
				cnt--;
				else if(s1[i]=='*')
				cnt++;
				else
				str+=s1[i];
			} 
			int sum=0;
			sum=q[str];
			if(sum>0)
			sum+=cnt;
			else
			sum=0;
			q[(string)s2]=sum;
		}
		else
		{
			string s1;
			cin>>s1;
			int cnt=0;
			string str="";
			for(int i=0;i<s1.size();i++)
			{
				if(s1[i]=='&')
				cnt--;
				else if(s1[i]=='*')
				cnt++;
				else
				str+=s1[i];
			}
			int sum=0;
			sum=q[str];
			if(sum>0)
			sum+=cnt;
			else
			sum=0;
			if(sum<=0)
				cout<<"errtype"<<endl;
			else
			{
				cout<<"void";
				for(int i=1;i<sum;i++)
				{
					cout<<"*";
				}
				cout<<endl;
			}
		}
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值