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;
    }



你可能感兴趣的:(NOJ [1114] Alice's Puppets)