zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Timer 实现2秒4秒连环炸
    Java中的注解
    PHP连接打印机
    php同步mysql两个数据库中表的数据
    thinkphp 两表、三表联合查询
    ereg/eregi报错处理办法
    ThinkPHP3.2判断手机端访问并设置默认访问模块的方法
    使用PHP获取时间今天 明天 昨天 时间戳的详解
    jquery获取radio和select选中值
    php开启mysqli扩展之后如何连接数据库
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852514.html
Copyright © 2011-2022 走看看