NOJ [1114] Alice's Puppets

  • 问题描述
  • Alice Margatroid (アリス·マーガトロイド) can control puppets. What's more, she can control her pupet to control other pupets.

    The puppets she controls directly called the  puppet. And the puppets which are controlled by the  puppets are called the  puppet. So as the  and the .
    Now your task is to tell me each puppet is what level.

  • 输入
  • This problem contains several cases.
    The first line of each case is an integer N (0 < N ≤ 1000), indicates the number of puppets.
    Then N lines follows. Each line has two strings, the name of the ith puppet and the controller's name. You can imagine if the controller is Alice, then the puppet is the first-level puppet.
    Every controller's name will be all appeared as a puppet's name before except Alice. Each name is no longer then 20.
  • 输出
  • For each case, you should output every puppet's names and levels, and order by level. If two puppets have the same level, then order by lexicographic.

    题意很简单,其实暴力就可以过,这里用了map,还有就是,我不是萝莉控,不懂日本少女漫画
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<iterator>
    #include<algorithm>
    using namespace std;
    
    struct node
    {
    	int rank;
    	char name[40];
    }pp[1010];
    
    int cmp(node a,node b)
    {
    	if(a.rank != b.rank)  
    	   return a.rank < b.rank;
    	else
    	{
    	 int rslt = strcmp(a.name,b.name); 
            if (rslt < 0) 
                return true;
            else 
                return false;  
            }
    }
    int main()
    {
    	int n;
    	char f[22],s[22];
    	while(~scanf("%d",&n))
    	{
    		map<string,int>puppet;
    		map<string,int>::iterator it;
    		int max=-1;
    		while(n--)
    		{
    			scanf("%s%s",f,s);
    			if(strcmp(s,"Alice")==0)
    			{
    				puppet[f]=1;
    				if(max<1)
    				   max=1;
    			}
    			else
    			{
    				it=puppet.find(s);
    				if(it!=puppet.end())
    				{
    					 puppet[f]=it->second+1;
    					 max++;
    				}
    		        else
    		        {
            			puppet[f]=max+1;
            		}
    			}
    		}
    		int k=0;
    		for(it=puppet.begin();it!=puppet.end();it++)
    		{
    			strcpy(pp[k].name,it->first.c_str());
    			pp[k++].rank=it->second;
    		}
    		sort(pp,pp+k,cmp);
    		for(int i=0;i<k;i++)
    		{
    			printf("%s %d\n",pp[i].name,pp[i].rank);
    		}
    	}
    	return 0;
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值