POJ 1094 Sorting It All Out

Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22106 Accepted: 7616

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

今天下午做题有点困,不在状态,这题目调了很长时间才调出来,一个简单的拓扑排序,但是要考虑各种情况,这是一道联系逻辑思维的好题目,在做题的时候必须要读懂题目,如果根据前面几组的数据确定了大小关系那么以后即使出现矛盾的也不用管。如果出现了环,立即确定矛盾就好了,最后才判断序列是不是被确定
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
class num
{
    public:
    int end;
    int next;
}a[1000000];
int b[30],res[30],status[30],top1,statck[30],sum;
int in[30],out[30],n;
string s1[100000];
int main()
{
    int deal();
    int i,j,m,s,t,x,k;
    int c1,c2;
    while(cin>>n>>m)
    {
        if(!n&&!m)
        {
            break;
        }
        for(i=0;i<=m-1;i++)
        {
            cin>>s1[i];
        }
        for(i=0;i<=m-1;i++)
        {
            memset(in,0,sizeof(in));
            memset(out,0,sizeof(out));
            memset(b,-1,sizeof(b));
            memset(status,0,sizeof(status));
            sum=0;
            for(j=0,x=0;j<=i;j++)
            {
                c1=s1[j][0]-65; c2=s1[j][2]-65;
                if(!status[c1])
                {
                  status[c1]=1;
                  statck[sum++]=c1;
                }
                if(!status[c2])
                {
                    status[c2]=1;
                    statck[sum++]=c2;
                }
                in[c2]++; out[c1]++;
                a[x].end=c2; a[x].next=b[c1];
                b[c1]=x; x++;
            }
            top1=0;
            k=deal();
            if(!k)
            {
                cout<<"Inconsistency found after "<<i+1<<" relations."<<endl;
                break;
            }else if(k==1)
            {
                memset(status,0,sizeof(status));
                for(j=0;j<=top1-1;j++)
                {
                    status[res[j]]=1;
                }
                for(j=0;j<=n-1;j++)
                {
                    if(!status[j])
                    {
                        break;
                    }
                }
                if(j==n)
                {
                    cout<<"Sorted sequence determined after ";
                    cout<<i+1<<" relations: ";
                    for(j=0;j<=n-1;j++)
                    {
                        cout<<(char)(res[j]+65);
                    }
                    cout<<"."<<endl;
                    break;
                }
            }
        }
        if(i==m)
        {
            cout<<"Sorted sequence cannot be determined."<<endl;
        }
    }
    return 0;
}
int deal()
{
    int i,j,base,top,k,x,s,k2;
    int queue[10000];
    base=top=0;
    k=0;k2=0;
    for(i=0;i<=sum-1;i++)
    {
        if(in[statck[i]]==0)
        {
            queue[top++]=statck[i];
            if(k==0)
            {
                k=1;
            }else if(k==1)
            {
                k2=1;
            }
        }
    }
    if(k==0)
    {
        return 0;
    }
    s=0;
    while(base<top)
    {
        x=queue[base++];
        res[top1++]=x; s+=1;
        k=0;
        for(j=b[x];j!=-1;j=a[j].next)
        {
            in[a[j].end]-=1;
            if(in[a[j].end]==0)
            {
                queue[top++]=a[j].end;
                if(k==0)
                {
                    k=1;
                }else if(k==1)
                {
                    k2=1;
                }
            }
        }
    }
    if(s==sum&&k2==0)
    {
        return 1;
    }else if(s<sum)
    {
        return 0;
    }else
    {
        return 2;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值