Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32746 | Accepted: 11374 |
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.
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.这个题就是给你一些关系,然后走到某一步是看看是否能够:
1.得到唯一确定的关系(点不能少)
2.出现了环(也就是关系错误)
3.不能确定的关系
这3个的优先级不难看出是先判断环2,因为子序有环最后必定有环,然后就是能唯一确定关系1,最后如果没有出现这两种情况,就是最后一种3了。
搞了很长时间终于是找到了可行的办法,学习了一个memcpy直接复制数组的值,哦嚯嚯。
这样的话,我们每读入一组关系,就进行一次拓扑排序,然后判断,是否出现2,然后判断3,出现了这两种的一个,便标记已经排序完,后面的数据只做读入操作,并不做处理。如果这两种都没有那就继续重复上面操作(排序,判断)。直到最后也没出现上面说的12两种情形,就是不能确定。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int inf=0x3f3f3f;
int pre[30];
int in[30],temp[30];
int n,m;
char w[30];
vector<int>head[30];
int panduan()
{
queue<int>q;
int i;
int flag=0;
int cnt=0;
for(i=0; i<n; ++i)
if(!in[i])
q.push(i);
while(!q.empty())
{
if(q.size()>1)flag=1;
int k=q.front();
q.pop();
w[cnt++]=k+'A';
int l=head[k].size();
for(i=0; i<l; ++i)
{
in[head[k][i]]--;
if(!in[head[k][i]])q.push(head[k][i]);
}
}
w[cnt]=0;
if(cnt<n)return 2;
if(flag)return 3;
return 1;
}
int main()
{
int i;
char s[4];
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m)break;
int stop=0;
for(i=0; i<n; ++i)
{
in[i]=0;
head[i].clear();
}
int ok=0;
int flag=3;
for(i=0; i<m; ++i)
{
scanf("%s",s);
if(ok)continue;
in[s[2]-'A']++;
head[s[0]-'A'].push_back(s[2]-'A');
memcpy(temp,in,sizeof(in));
flag=panduan();
memcpy(in,temp,sizeof(temp));
if(flag!=3)
{
ok=1;
stop=i+1;
}
}
if(flag==2)printf("Inconsistency found after %d relations.\n",stop);
else if(flag==1)printf("Sorted sequence determined after %d relations: %s.\n",stop,w);
else
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}