POJ-1094 Sorting it All Out

来源:互联网 发布:软件模块化设计原则 编辑:程序博客网 时间:2024/06/05 15:26
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 6A<BA<CB<CC<DB<DA<B3 2A<BB<A26 1A<Z0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.

Sorted sequence cannot be determined.

这道题是一道典型的拓扑排序,如果一个字母小于另外一个字母,则入度加一,并把小的当成大的字母的一个的出度,如果有一个入度为0就输出,并把其其所有出度点减一个入度,循环n次,若一次循环出两个字母,则排序失败,但不跳出,因为一旦遇到了一次循环根本无法输出,则输入结束后输出Inconsistency found after t relations.,t为这条指令的序号(每输入一个指令就判断),当然,若当次就能判断顺序,我们就把t和输出顺序存下来,到最后再输出。

注意:如果排序已经成功,则不用再管有没有回路的情况,若已存在回路的情况,则不再管排序能不能成功,所以,当已经存在回路或已经成功排序时就不用再做处理了。

#include<iostream>using namespace std;int n,m,sh=0,a[1001]={0},t,c[10001]={0},b[101][101]={0},loc[1001]={0},d[10001]={0};int a1[10000001],b1[10000001];int main(){string s;while(cin>>n>>m&&n!=0&&m!=0){//只要还在输入for(int i=1;i<=m;i++){     cin>>s;、、输入指令     if(s[1]=='>'){//如果中间的是大于b[(s[0]-'A'+1)][0]++;b[(s[0]-'A'+1)][b[(s[0]-'A'+1)][0]]=(s[2]-'A'+1);b[(s[0]-'A'+1)][b[(s[0]-'A'+1)][0]]=i;a[(s[2]-'A'+1)]++;d[(s[2]-'A'+1)]++;}else {//如果中间的是小于b[(s[2]-'A'+1)][0]++;b[(s[2]-'A'+1)][b[s[2]-'A'+1][0]]=(s[0]-'A'+1);a[(s[0]-'A'+1)]++;d[(s[0]-'A'+1)]++;}if(sh==0&&z==0){//如果还没排出序而且还不存在回路,则进行模拟bool panda=0;for(int l=1;l<=n;l++){//要用其他东西存起来,不然排序失败就完了c[l]=a[l];}int BS=0;for(int s=1;s<=n;s++){int ans=0;for(int j=1;j<=n;j++){ if(a[j]==0){ c[j]--; BS++;//输出的字母的个数加1 ans++; if(ans==2){//有多个入度为0的则排序失败,但还有继续排序 panda=1; } loc[s]=j;//输出顺序 int k=b[j][0];  for(int t=1;t<=k;t++){     c[b[j][t]]--; } } if(ans==2){ panda=1; }}for(int l=1;l<=n;l++){a[l]=c[l];}if(ans==0&&BS!=n){//如果不存在入度为0的点且还没有输出完,则存在回路sh=2;t=i;panda=1;break;}else if(ans>=2){panda=1;} }if(panda==0){//排序成功if(z==0)z=i;}}for(int ss=1;ss<=n;ss++){a[ss]=c[ss]=d[ss];}}if(z!=0){//输出cout<<"Sorted sequence determined after "<<z<<" relations: ";for(int ll=n;ll>=1;ll--)cout<<char(loc[ll]+'A'-1);cout<<"."<<endl;}else {if(sh==0)cout<<"Sorted sequence cannot be determined."<<endl;else if(sh==2)cout<<"Inconsistency found after "<<t<<" relations."<<endl;}sh=0;z=0;for(int i=1;i<=26;i++){//清0for(int j=0;j<=26;j++){b[i][j]=e[i][j]=0;vis[i][j]=0;}}for(int t=1;t<=26;t++){a[t]=c[t]=d[t]=0;}}}

原创粉丝点击