zoukankan      html  css  js  c++  java
  • (poj1094)Sorting It All Out

    Description

    An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

    Input

    Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

    Output

    For each problem instance, output consists of one line. This line should be one of the following three:

    Sorted sequence determined after xxx relations: yyy...y.
    Sorted sequence cannot be determined.
    Inconsistency found after xxx relations.

    where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.

    Sample Input

    4 6
    A<B
    A<C
    B<C
    C<D
    B<D
    A<B
    3 2
    A<B
    B<A
    26 1
    A<Z
    0 0
    

    Sample Output

    Sorted sequence determined after 4 relations: ABCD.
    Inconsistency found after 2 relations.
    Sorted sequence cannot be determined.

    Hint

    #include<cstdio>  
    #include<cstring>  
    #include<queue>  
    #include<vector>  
    using namespace std;  
    const int maxn=26+5;  
    vector<int> G[maxn];  
    int in[maxn];  
    int n,m;  
    char order[1000][10];  
    char ans[maxn];  
    int cnt;
    int topo ()
    {int i;
    cnt=0;bool p=true;
    queue<int> q;  
        int in1[maxn];
    memcpy(in1,in,sizeof(in));  
    for(i=0;i<n;i++)
    if(in1[i]==0)q.push(i);
    while(!q.empty())
    {
    if(q.size()>1)p=false;
    int u=q.front();q.pop();
    ans[cnt++]=u+'A';
    for(i=0;i<G[u].size();i++)
    {int v=G[u][i];
    if(--in1[v]==0)q.push(v);
    }
    }
    int p1=0;
    if(cnt<n) p1 = -1;
        else if(p==1) p1=1; 
        return p1;


    }

    int main()
    {
    while(scanf("%d%d",&n,&m)==2&&n)  
        {  int i;
            memset(in,0,sizeof(in));  
            for(i=0;i<n;i++) G[i].clear();  
            for(i=0;i<m;i++)  
                scanf("%s",order[i]);  
            int flag=0;  
            for(i=0;i<m;i++)  
            {  
                int u=order[i][0]-'A', v=order[i][2]-'A';  
                G[u].push_back(v);  
                in[v]++;  
                if( (flag=topo())!=0) break;  
            }  
            ans[n]=0;      
            if(flag==1) printf("Sorted sequence determined after %d relations: %s. ",i+1,ans);  
            else if(flag==0) printf("Sorted sequence cannot be determined. ");  
            else if(flag==-1) printf("Inconsistency found after %d relations. ",i+1);  
        }  
        return 0;  
    }  


    第一步  ans  AB->CD;
    第二步           ABC->D
    第三步           ABCD


  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.3.13
    Elementary Methods in Number Theory Exercise 1.3.17, 1.3.18, 1.3.19, 1.3.20, 1.3.21
    数论概论(Joseph H.Silverman) 习题 5.3,Elementary methods in number theory exercise 1.3.23
    Elementary Methods in Number Theory Exercise 1.2.31
    数论概论(Joseph H.Silverman) 习题 5.3,Elementary methods in number theory exercise 1.3.23
    Elementary Methods in Number Theory Exercise 1.3.13
    Elementary Methods in Number Theory Exercise 1.3.17, 1.3.18, 1.3.19, 1.3.20, 1.3.21
    Elementary Methods in Number Theory Exercise 1.2.31
    Elementary Methods in Number Theory Exercise 1.2.26 The Heisenberg group
    4__面向对象的PHP之作用域
  • 原文地址:https://www.cnblogs.com/lengxia/p/4387802.html
Copyright © 2011-2022 走看看