• [POJ] 1094 Sorting It All Out


    Sorting It All Out
    Time Limit: 1000MS      Memory Limit: 10000K
    Total Submissions: 37417        Accepted: 13220
    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.
    Source
    
    East Central North America 2001

    最失败的题之一,调了太久,很难受。

    topo()的返回值写成bool,查良久,无果。。

    本质就是拓扑排序,把字母当成节点,建图,不过加入了几个特殊的条件,有环或者无法完全排序都是特殊情况,要特殊判断,但是判断完了剩下的数据仍然得输入。

    然后输出的部分,句号什么的,很难受。写的时候也是很混乱..

    可能今天心情不好…

    //Stay foolish,stay hungry,stay young,stay simple
    #include<iostream>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int MAXN=20000;
    
    int n,m;
    
    struct Edge{
        int next,to;
    }e[MAXN];
    int ecnt,head[MAXN],ind[MAXN];
    inline void add(int x,int y){
        e[++ecnt].next = head[x];
        e[ecnt].to = y;
        head[x] = ecnt;
    }
    int tmp[MAXN];
    int ans[MAXN],anscnt;
    int topo(){
        anscnt=0;
        memcpy(tmp,ind,sizeof(ind));
        int ret=1;
        for(int i=1;i<=n;i++){
            int cnt=0,pos;
            for(int j=1;j<=n;j++)
                if(tmp[j]==0) cnt++,pos=j;
            if(cnt==0) return 0;//circle
            if(cnt>1) ret=-1;//unsure 
            tmp[pos]=-1;//
            ans[++anscnt]=pos;
            for(int j=head[pos];j;j=e[j].next){
                int v=e[j].to;
                tmp[v]--;
            }
        }
        return ret;
    }
    
    
    
    void slove(){
        bool succ=0;
        for(int i=1;i<=m;i++){
            char s[5];
            cin>>s;
            if(succ) continue;
            int x=s[0]-'A'+1;
            int y=s[2]-'A'+1;
            add(x,y);
            ind[y]++;
    
            int res=topo();
    
            if(res==0){
                printf("Inconsistency found after %d relations.
    ",i);
                succ=1;
            }
            if(res==1){
                printf("Sorted sequence determined after %d relations: ",i);
                for(int j=1;j<=anscnt;j++) cout<<char(ans[j]+'A'-1);
                printf(".
    ");
                succ=1;//
            }
        }
        if(!succ) printf("Sorted sequence cannot be determined.
    ");
    }
    
    int main(){
    //  freopen("test.txt","r",stdin);
        while(cin>>n>>m&&n){
            ecnt=0,anscnt=0;
            memset(head,0,sizeof(head));
            memset(ind,0,sizeof(ind));
            slove();
        }
        return 0;
    }
    

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247451.html

  • 相关阅读:
    Winform中用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity
    C# 生成二维码
    T-sql语句修改数据库逻辑名、数据库名、物理名
    ASP.NET MVC中使用jQuery时的浏览器缓存问题
    关于asp.net页面缓存
    关于VS 工具箱灰色,不可用的解决方案
    Android
    如何让一个DIV水平,垂直方向都居中于浏览器?
    cookie.setPath()的用法
    CSS选择器
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9247451.html
Copyright © 2020-2023  润新知