博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1094
阅读量:6575 次
发布时间:2019-06-24

本文共 2650 字,大约阅读时间需要 8 分钟。

Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26911   Accepted: 9285

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

Sample Output

Sorted sequence determined after 4 relations: ABCD.Inconsistency found after 2 relations.Sorted sequence cannot be determined.

Source

AC代码:

#include
#include
#include
#include
#include
#include
using namespace std;int n,m;int in[30],tmp_in[30];int f1,f2;int str[30];vector
G[30];void top_sort(){ //拓扑排序 f1=0; //没有环 f2=1; //唯一排序 for(int i=0;i
s; while(!s.empty()) s.pop(); for(int i=0;i
=2){ f2=0; } int tmp=s.top(); s.pop(); str[counter++]=tmp; for(int i=0;i
>n>>m&&(n!=0||m!=0)){ memset(in,0,sizeof(in)); int i; for(i=0;i
>ch; G[ch[0]-'A'].push_back(ch[2]-'A'); in[ch[2]-'A']++; top_sort(); if(f1){ cout<<"Inconsistency found after "<
<<" relations."<
>ch; break; } else if(f2){ cout<<"Sorted sequence determined after "<
<<" relations: "; for(int j=0;j
>ch; break; } } if(i>=m) cout<<"Sorted sequence cannot be determined."<

版权声明:本文博客原创文章,博客,未经同意,不得转载。

你可能感兴趣的文章
蓝牙手柄按键码
查看>>
redis启动失败
查看>>
Navicat for PostgreSQL 怎么维护数据库和表
查看>>
java并发库之Executors常用的创建ExecutorService的几个方法说明
查看>>
Spring框架错误之org.springframework.beans.factory.BeanCreationException
查看>>
23种设计模式(1):单例模式
查看>>
socket 编程入门教程(五)UDP原理:4、“有连接”的UDP
查看>>
linux sort 命令详解
查看>>
Jquery获取iframe中的元素
查看>>
Laravel 学习笔记5.3之 Query Builder 源码解析(下)
查看>>
Struts2简单入门实例
查看>>
Android应用及应用管理
查看>>
2012CSDN年度博客之星评选http://vote.blog.csdn.net/item/blogstar/xyz_lmn
查看>>
Linux系统与网络服务管理技术大全(第2版)
查看>>
window下配置定时任务实现类似linux的cron定时任务
查看>>
铁道部否认被中铁工程等十多家公司老总蹲点讨债
查看>>
js事件---事件流
查看>>
我的友情链接
查看>>
谁拿了最多奖学金
查看>>
详解linux运维工程师入门级必备技能
查看>>