cf试做1(补题)

  B - Vasya and Types

    题意很长,很麻烦的判断,给typedef a b  和  typeof a  两种操作,void 和errtype 两种类型,开始我考虑到每种情况的判断,细节很多,没有解出来,后来看人家的代码,问题其实没那么复杂,看输出就知道,只有errtype 和 void+若干个* 两种,所以在求得时候至于要用mp<string, int>维护*的个数就好了。注意的是这里初始化mp["viod"]=1,输出时mp-1个‘*’结束。

代码:

#include<iostream>
#include<map>
#include<string>
using namespace std;
map<string, int>mp;// represent the num of '*'
int solve(string a)
{
    string temp="";
    int num=0;
    int len=a.size();
    for(int i=0; i<len; i++)
    {
        if(a[i]=='&')
            num--;
        else if(a[i]=='*')
            num++;
        else
            temp+=a[i];
    }
    int cnt=mp[temp];
    int ans=0;
    if(cnt>0)
    {
        ans=cnt+num;
    }
    else//原来 是 errtype
    {
        ans=0;
    }
    return ans;
}
int main()
{
    int N;
    int num;
    string ch, a, b;
    mp.clear();
    mp["void"]=1;//初始化 !! 很重要!!
    cin>>N;
    while(N--)
    {
        cin>>ch;
        if(ch=="typedef")
        {
            cin>>a>>b;
            num=solve(a);
            if(num<=0)
                mp[b]=0;
            else
                mp[b]=num;
        }
        else if(ch=="typeof")
        {
            cin>>a;
            num=solve(a);
            num--;
            if(num<0)
                cout<<"errtype"<<endl;
            else
            {
                cout<<"void";
                for(int i=1; i<=num; i++)
                    cout<<"*";
                cout<<endl;
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值