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. Source |
提示
题意:
给你几个字母比较顺序,按照以下规则输出:
1:Sorted sequence determined after xxx relations: yyy.(xxx为第几次输入得到的结果,yyy为通过输入的比较信息确定的序列,从小到大输出)
2:Inconsistency found after xxx relations.(xxx同上,如果字母间大小有矛盾输出它)
3:Sorted sequence cannot be determined.(如果通过输入的比较信息不能确定唯一的序列,则输出它)
思路:
这算是对拓扑排序熟练程度的考验了。
第1句对应基本拓扑排序的输出。
第2句对应拓扑排序合不合法,即有没有成环。
第3句对应拓扑排序唯不唯一,也就是度为0的点只有一个。
最后注意细节。
示例程序
Source Code
Problem: 1094 Code Length: 1663B
Memory: 388K Time: 16MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
int f(int x,int n,int in[],int map[26][26])
{
int i,i1,t[26],q[26],top=0,f,pos,bug=1;
for(i=0;n>i;i++)
{
t[i]=in[i]; //需要复制一份度的数据,不能破坏先前的数据,以后还有可能需要再做判断
}
for(i=0;n>i;i++)
{
f=0;
for(i1=0;n>i1;i1++) //找度为0的节点,并记录个数
{
if(t[i1]==0)
{
f++;
pos=i1;
}
}
if(f==0) //成环了
{
return 0;
}
if(f>1) //序列还不唯一,但还需在做判断,例如n>2时,会存在有A>B,B>A这样的小环却没有进行判断
{
bug=-1;
}
q[top]=pos;
top++;
t[pos]=-1;
for(i1=0;n>i1;i1++)
{
if(map[pos][i1]==1)
{
t[i1]--;
}
}
}
if(bug==1)
{
printf("Sorted sequence determined after %d relations: ",x);
for(i=0;top>i;i++)
{
printf("%c",q[i]+'A');
}
printf(".\n");
return 1;
}
return -1;
}
int main()
{
int n,m,in[26],i,k,map[26][26];
char ch[4];
scanf("%d %d",&n,&m);
while(n!=0||m!=0)
{
k=-1;
memset(in,0,sizeof(in));
memset(map,0,sizeof(map));
for(i=1;m>=i;i++) //因为题目要求输出的几次有相应结果,所以每输入一次就判断一次
{
scanf("%s",ch);
if(k==1)
{
continue;
}
in[ch[2]-'A']++; //度的记录
map[ch[0]-'A'][ch[2]-'A']=1; //存图
k=f(i,n,in,map); //判断函数
if(k==0)
{
printf("Inconsistency found after %d relations.\n",i);
k=1;
}
}
if(k==-1)
{
printf("Sorted sequence cannot be determined.\n");
}
scanf("%d %d",&n,&m);
}
return 0;
}