poj1094 Sorting It All Out
来源:互联网 发布:php程序员是什么意思 编辑:程序博客网 时间:2024/06/07 15:08
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 36044 Accepted: 12680
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 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
题意:给很多的两两数字之间的大小关系(e.g A<B ),然后问你能不能给出他们的总的排序。
题解:拓扑排序。每次输入一个关系,就进行一次拓扑排序:
1.如果排序唯一,那么输出第一种情况
2.如果出现了环,那么输出第二种情况
3.如果到了输入完毕后,排序仍不唯一,那么输出第三种情况
一开始我让它中途出现了排序不唯一直接输出cannot be determined,后来发现sb了,直到所有东西都输完了才能确定能否唯一确定。情况1和2是一旦中途出现,就可以输出了。
代码如下:
#include<stdio.h>#include<queue>#include<vector>#include<cstring>#define SORTED 1#define INCON 2#define CANTDERTER 3using namespace std;const int MAX = 300;vector <int> G[MAX];int indegree[MAX];int temp[MAX]; //备份indegree int pos[MAX]; //输出 int n, m;int cnt;/*输入一个,就进行一次拓扑排序1.如果排序唯一,那么输出第一种情况2.如果出现了环,那么输出第二种情况3.如果到了输入完毕后,排序仍不唯一,那么输出第三种情况 */ int topo(){queue<int> q;cnt = 0;int unsure = 0;for(int i = 1;i <= n; ++i){if(!indegree[i]){q.push(i);}}while(!q.empty()){if(q.size() > 1)unsure = 1;int v = q.front();q.pop();pos[cnt++] = v;for(int i = 0;i < G[v].size();++i){if(!--indegree[G[v][i]]){q.push(G[v][i]);}}}if(cnt < n) return INCON;if(unsure) return CANTDERTER;return SORTED;} int main(){char ch1,ch2,ch3;int x,y;int ok, res;while(scanf("%d%d",&n,&m) == 2 && (n||m)){memset(pos,0,sizeof(pos));memset(indegree,0,sizeof(indegree));for(int i = 0;i < MAX; ++i)G[i].clear();ok = 0;for(int i = 1;i <= m; ++i){while(getchar()!='\n')continue;scanf("%c%c%c",&ch1,&ch3,&ch2);if(ok) continue;int x = ch1 - 'A' + 1, y = ch2 - 'A' + 1;G[x].push_back(y);++indegree[y];//备份indegreememcpy(temp,indegree,sizeof(indegree)); res = topo();//还原indegreememcpy(indegree,temp,sizeof(indegree)); if(res == SORTED) {ok = 1;printf("Sorted sequence determined after %d relations: ",i);for(int j = 0;j < cnt; ++j)printf("%c",pos[j]+'A'-1);printf(".\n"); }else if(res == INCON){ok = 1;printf("Inconsistency found after %d relations.\n",i);}else continue;}if(res == CANTDERTER)printf("Sorted sequence cannot be determined.\n");}}
阅读全文
0 0
- POJ1094 Sorting It All Out
- poj1094 Sorting It All Out
- poj1094 Sorting It All Out
- poj1094 Sorting It All Out
- Sorting It All Out poj1094
- POJ1094 Sorting It All Out
- poj1094 Sorting It All Out
- poj1094--Sorting It All Out
- POJ1094 - Sorting It All Out
- poj1094 Sorting It All Out
- POJ1094 Sorting It All Out
- Sorting It All Out-POJ1094
- poj1094 Sorting It All Out
- poj1094 Sorting It All Out
- POJ1094 Sorting It All Out
- poj1094 Sorting It All Out
- poj1094——Sorting It All Out
- poj1094 Sorting It All Out (拓扑排序)
- WIN DLL
- IDEA中搭建struts2项目
- 11 cookie&&session的学习笔记
- Linux下查看文件精确到秒的修改时间
- poj 3468 线段树
- poj1094 Sorting It All Out
- js中for循环的作用域;函数调用的理解;数组对象的理解;
- 输出乘法顺序表
- 求C_18^13的值
- hdu 6006 Engineer Assignment(状压DP)
- spring注解
- docker本机启动多台容器导致出现后续容器启动失败
- 留言板功能
- python 修饰器