Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28762 | Accepted: 9964 |
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.
- Source Code
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; int num[201]; int n,m; int map[301][301]; int v[301]; int count1; int a[301]; int TP1() { int pcount = count1; int cnt = 0; int pm[301]; memset(pm,0,sizeof(pm)); for(int pa='A'; pa<='Z'; pa++) { pm[pa] = 0; pm[pa] = num[pa]; } for(int pa=0; pa<n; pa++) { int f1 = 0; for(int pb='A'; pb<='Z'; pb++) { if(pm[pb] == 0 && v[pb]) { f1 = 1; pm[pb]--; pcount--; for(int pc='A'; pc<='Z'; pc++) { if(map[pc][pb] == 1) { pm[pc]--; } } break; } } if(f1 == 0 && pcount) { return 0; } } return 1; } int TP() { memset(a,0,sizeof(a)); int pn = 0; int mm[201]; int cnt = 0; for(int pi='A'; pi<='Z'; pi++) { mm[pi] = 0; mm[pi] = num[pi]; } for(int pi='A'; pi<='Z'; pi++) { if(mm[pi] == 0 && v[pi]) { cnt++; } } if(cnt>1) { return 0; } if(cnt == 0) { return -1; } for(int pi=0; pi<n; pi++) { for(int pj='A'; pj<='Z'; pj++) { if(mm[pj] == 0 && v[pj]) { a[pn++] = pj; mm[pj]--; count1--; cnt = 0; for(int pk='A'; pk<='Z'; pk++) { if(map[pk][pj] == 1) { mm[pk]--; if(mm[pk] == 0) { cnt++; } } } if(cnt>1) { return 0; } if(cnt == 0 && count1) { return -1; } break; } } } if(count1 == 0 && pn == n) { return 1; } else if(count1 == 0 && pn!=n) { return 0; } else { return -1; } } int b[30]; int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n == 0 && m == 0) { break; } int pf = 0,flag = 0,pi1; memset(b,0,sizeof(b)); char str[10]; memset(num,0,sizeof(num)); memset(v,0,sizeof(v)); memset(map,0,sizeof(map)); for(int i=0; i<m; i++) { scanf("%s",str); if(map[str[0]][str[2]] == 0) { num[str[0]]++; v[str[0]]++; v[str[2]]++; map[str[0]][str[2]] = 1; } count1 = 0; for(int j='A'; j<='Z'; j++) { if(v[j]) { count1++; } } int pk; if(flag!=1) { if(TP1() && pf == 0) { pk = TP(); if(pf == 0) { if(pk == -1) { pf = 1; flag = 2; pi1 = i+1; } else if(pk == 1 && flag == 0) { flag = 1; pi1 = i+1; for(int l=0; l<n; l++) { b[l] = a[l]; } } } } else { if(pf == 0) { pi1 = i+1; flag = 2; pf = 1; } } } } if(flag == 0) { printf("Sorted sequence cannot be determined.\n"); } else if(flag == 1) { printf("Sorted sequence determined after %d relations: ",pi1); for(int pj=n-1; pj>=0; pj--) { printf("%c",b[pj]); } printf(".\n"); } else if(flag == 2) { printf("Inconsistency found after %d relations.\n",pi1); } } return 0; }