Problem 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: <br> <br>Sorted sequence determined after xxx relations: yyy...y. <br>Sorted sequence cannot be determined. <br>Inconsistency found after xxx relations. <br> <br>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. <br>
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.
先不说怎么做了,这题一个毛病是输出不是在输入完之后,而是可能才输入了一部分数据就输出了!第一次见这种操作,什么毛病,搞不明白。这是个拓扑排序的问题,但是我按拓扑入栈出栈的一套写不对,这个题的三种情况我判断不全。然后看了看人家的做法,感觉这一个可以当做拓扑排序的另一个模板来记住,是用于判断能不能得到一个确定的结果。(原来的拓扑排序出来是可能排出来好几种,比方a<b,a<c,b<d,c<d,可以排出)。下面是代码:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int n,m;
int r[30],c[30];
int p[30][30];
int ans[30]; //存排序结果
int ioan()
{
int flag=1;
int cnt=0;
int rr[30];
int cc[30];
for(int i=1;i<=n;i++)//用cc【】、rr【】,防止更改数据
{
rr[i]=r[i];
cc[i]=c[i];
}
for(int i=1;i<=n;i++)
{
int r0=0,k=0;
for(int j=1;j<=n;j++)
{
if(rr[j]==0)
{
r0++;
k=j;
}
}
if(r0==0)
{
flag=0;
return flag; //表明有环 直接 return
}
if(r0>1) //表明无序
{
flag=-1;
}
cnt++;
ans[cnt]=k; //存入 k
rr[k]=-1; //标记 k
for(int j=1;j<=cc[k];j++)
{
int y=p[k][j];
rr[y]--;
}
}
return flag; //最后return flag 等于-1则无序 等于1则表示排完了
}
int main()
{
char a,b;
int x,y;
while(cin>>n>>m)
{
if(n==0 && m==0)
break;
int flag=0;
memset(r,0,sizeof(r));
memset(c,0,sizeof(c));
memset(p,0,sizeof(p));
for(int i=1;i<=m;i++)
{
cin>>a>>b>>b;
if(flag==1)
{
continue;
}
x=a-'A'+1;
y=b-'A'+1;
c[x]++;
p[x][c[x]]=y;
r[y]++;
int f=ioan();
if(f==0)
{
flag=1;
cout<<"Inconsistency found after "<<i<<" relations."<<endl;
}
if(f==1)
{
flag=1;
cout<<"Sorted sequence determined after "<<i<<" relations: ";
for(int i=1;i<=n;i++)
cout<<char(ans[i]+'A'-1);
cout<<"."<<endl;
}
}
if(flag==0)
cout<<"Sorted sequence cannot be determined."<<endl;
}
return 0;
}