• Gym


    http://codeforces.com/gym/102219/problem/J

    You are given 5 different sizes of kitchen plates. Each plate is marked with a letter A, B, C, D, or E. You are given 5 statements comparing two different plates, you need to rearrange the plates from smallest size to biggest size. For example: the sizes of these plates.

    Input

    The input consist of 5 lines. In each line there will be 3 characters, the first and last character will be either A, B, C, D, or E and the middle character will be either > or <

    describing the comparison between two plates sizes. No two plates will be equal.

    Output

    The output consist of 5

    characters, the sorted order of balls from smallest to biggest plate. Otherwise, if the statements are contradicting print impossible. If there are multiple answers, print any of them.

    Input

    D>B
    A>D
    E<C
    A>B
    B>C
    

    Output

    ECBDA
    

    Input

    B>E
    A>B
    E>A
    C<B
    D<B
    

    Output

    impossible
    

     题意分析:

    给出5个A, B, C, D, E两者之间的关系,求五者的从小到大排序。

    解题思路:

    拓扑排序

    #include <stdio.h>
    #include <string.h>
    #define N 10
    char str[10], ans[N];
    int indegree[N], e[N][N];
    bool book[N];
    int main()
    {
    	while(scanf("%s", str)!=EOF){
    		memset(e, 0, sizeof(e));
    		memset(indegree, 0, sizeof(indegree));
    		memset(book, false, sizeof(book));
    		
    		if(str[1]=='<'){
    			indegree[str[0]-'A']++;
    			e[str[2]-'A'][str[0]-'A']++;
    		}else{
    			indegree[str[2]-'A']++;
    			e[str[0]-'A'][str[2]-'A']++;
    		}
    		for(int i=1; i<=4; i++)
    		{
    			scanf("%s", str);
    			if(str[1]=='<'){
    			indegree[str[0]-'A']++;
    			e[str[2]-'A'][str[0]-'A']++;
    			}else{
    				indegree[str[2]-'A']++;
    				e[str[0]-'A'][str[2]-'A']++;
    			}
    		}
    		int len=0;
    		//temp=1;
    		while(1)
    		{
    			int temp=0;
    			for(int i=0; i<5; i++)	
    				if(indegree[i]==0 && book[i]==false)
    				{
    					temp=1;
    					book[i]=true;
    					ans[len++]=i+'A';
    					for(int j=0; j<5; j++)
    					{
    						if(e[i][j])
    						{
    							e[i][j]--;
    							indegree[j]--;
    						}
    					}
    					break;
    				}
    			if(temp==0)
    				break;
    		}
    		if(len==5)
    		{
    			for(int i=len-1; i>=0; i--)
    				printf("%c", ans[i]);
    			printf("
    ");
    		}
    		else printf("impossible
    ");
    				
    	} 
    	return 0;
    }
  • 相关阅读:
    多线程(6)线程属性
    多线程(五) Thread和Object中线程相关方法
    面试汇总
    多线程(4)线程生命周期
    多线程(3) 多线程之线程的停止和中断
    springboot(6)redis缓存
    软件安装(总)
    redis分布式锁
    第一天
    Thinkphp5高级进阶教程
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852514.html
Copyright © 2020-2023  润新知