首页 > 代码库 > poj 1094 / zoj 1060 Sorting It All Out
poj 1094 / zoj 1060 Sorting It All Out
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 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. Source East Central North America 2001 |
[Submit] [Go Back] [Status] [Discuss]
解题思路:这题是一个拓扑排序问题,不过过程比较复杂,要处理的情况也比较多!首先题目条件和要求要很清楚,这样分析问题思路就会清晰!
解题代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 using namespace std; 5 struct Node{ //与之相连的节点结构体 6 int to; 7 struct Node *next; 8 }; 9 Node *Link[27]; //链表保存连接关系 10 Node *tm_node; 11 bool vis[27]; 12 int count[27], tm_count[27]; //保存节点入度数据,前者为备份数据,后者为运算数据 13 int n, m, num; 14 char deal[3]; 15 const int A = ‘A‘; 16 int result[27], pos; //保存结果 17 18 int TopSort(){ 19 int i, j, cnt; 20 bool flag = false; 21 pos = cnt = 0;// cnt为入度为0的节点数,pos为保存结果的下标 22 j = -1; //入度为0的字母 23 for (i = 0; i < n; i ++) //寻找入度为0的位置 24 if(tm_count[i] == 0 && vis[i]){ 25 cnt ++; 26 j = i; 27 } 28 while (~j){ 29 if(cnt > 1) //如果cnt > 1 则表示有多个入度为0的节点,也就是说可能无法排序 30 flag = true; //不直接return是因为还有可能图中有环,导致数据矛盾 31 for (tm_node = Link[j]; tm_node != NULL; tm_node = tm_node ->next) 32 tm_count[tm_node ->to] --; 33 result[pos ++] = j; 34 tm_count[j] --; 35 j = -1; 36 cnt = 0; 37 for (i = 0; i < n; i ++) 38 if(tm_count[i] == 0 && vis[i]){ 39 cnt ++; 40 j = i; 41 } 42 } 43 if(!flag && pos != num) // 图中有环,数据矛盾 44 return -1; 45 if(!flag && pos == num) //图中为一个有序序列,是否是最终结果还需进一步判断 46 return 0; 47 if(flag && pos == num) //图中有多个有序序列,还需进一步判断 48 return 1; 49 if(flag && pos != num) //图中有环,数据矛盾 50 return -1; 51 } 52 53 void init(){ 54 num = 0; 55 memset(vis, 0, sizeof(vis)); 56 memset(Link, 0, sizeof(Link)); 57 memset(count, 0, sizeof(count)); 58 memset(tm_count, 0, sizeof(tm_count)); 59 } 60 61 void input(){ 62 int d1, d2; 63 scanf("%s", deal); 64 d1 = deal[0] - A; 65 d2 = deal[2] - A; 66 tm_node = new Node; 67 tm_node ->next = NULL; 68 tm_node ->to = d2; 69 count[d2] ++; 70 if(Link[d1] == NULL) 71 Link[d1] = tm_node; 72 else{ 73 tm_node ->next = Link[d1]; 74 Link[d1] = tm_node; 75 } 76 if(vis[d1] == 0){ 77 num ++; 78 vis[d1] = true; 79 } 80 if(vis[d2] == 0){ 81 num ++; 82 vis[d2] = true; 83 } 84 } 85 86 int main(){ 87 int i, j; 88 int value; 89 bool had_ans; 90 while(~scanf("%d%d", &n, &m) && (n && m)){ 91 init(); //初始化 92 had_ans = false; //是否已经得出结果 93 for(i = 1; i <= m; i ++){ 94 if(had_ans){ //如果已有结果,则无须处理后续数据 95 scanf("%s", deal); 96 continue; 97 } 98 input(); //数据处理 99 for(j = 0; j < n; j ++)100 tm_count[j] = count[j]; 101 value = http://www.mamicode.com/TopSort(); //拓扑排序 102 if (value =http://www.mamicode.com/= -1){ 103 printf ("Inconsistency found after %d relations.\n", i);104 had_ans = true;105 }106 else if(value =http://www.mamicode.com/= 0 && num == n){ //数据量以满足要求,且能排序 107 printf ("Sorted sequence determined after %d relations: ", i);108 for (j = 0; j < n; j ++)109 printf ("%c", result[j] + A);110 printf(".\n");111 had_ans = true;112 }113 }114 if (value =http://www.mamicode.com/= 1 || (value =http://www.mamicode.com/= 0 && n != num)) // 无法排序115 printf("Sorted sequence cannot be determined.\n");116 }117 return 0;118 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。