Sorting It All Out

描述

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 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.

输出

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.

样例输入

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

样例输出

Sorted sequence determined after 4 relations: ABCD.

Inconsistency found after 2 relations.

Sorted sequence cannot be determined.

题意

给你n个字符和m个关系,当处理到第几个关系时,就能确定这n个字符的大小关系,若发现有矛盾的情况就不再处理了并且记录下在第几关系式发现的矛盾,若处理到最后还没有确定这n个字符的关系,就输出Sorted sequence cannot be determined.,若能确定这n个字符的关系,就输出在第几个关系中确定的,并输出他们的大小关系。

分析

对于每次给出的关系都进行一次拓扑排序,来判断是否已形成环(出现矛盾关系),能够确定这些字符的关系,必定只能在这些关系中只有一个点的入度为零,否则就不能确定这些字符的关系。

代码

#include<string>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stack>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
int du[30];
vector<int>edge[30];
char q1[30];
int topSort(int n)
{
    int du1[30];
    for(int i=0; i<n; i++)
        du1[i]=du[i];

    queue<int>q;
    for(int i=0; i<n; i++)
    {
        if(du1[i]==0)q.push(i);
    }
    int op1=1;
    int k=0;
    while(!q.empty())
    {
        if(q.size()>1)op1=0;//每个点的入度只能唯一,否则就不能就不能确定其关系
        int a=q.front();
        q1[k++]=a+'A';
        q.pop();
        for(int i=0; i<edge[a].size(); i++)
        {
            int b=edge[a][i];

            du1[b]--;
            if(du1[b]==0)
            {
                q.push(b);
            }
        }
    }
    if(k<n)return -1;//出现负环
    return op1;

}

int main()
{
    //freopen("2.txt","r",stdin);
    int m,n;
    while(scanf("%d%d",&m,&n),m,n)
    {
        memset(du,0,sizeof(du));
        memset(q1,'\0',sizeof(q1));
        for(int i=0;i<30;i++)
            edge[i].clear();
        int flag=0;;
        for(int i=0; i<n; i++)
        {
            char a,b;
            scanf(" %c<%c",&a,&b);
            if(flag!=0)
                continue;
            edge[a-'A'].push_back(b-'A');
            du[b-'A']++;
            flag=topSort(m);
            if(flag==-1)
            {
                printf("Inconsistency found after %d relations.\n",i+1);
                continue;
            }
            if(flag==1)
            {
                printf("Sorted sequence determined after %d relations: %s.\n",i+1,q1);
                continue;
            }
        }
        if(flag==0)
            printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}
posted @ 2017-04-19 13:45  -梦里不知身是客  阅读(124)  评论(0编辑  收藏  举报