每次加入一个关系都要进行拓扑排序,不过在排序过程中需要判断是否出现多个入度为0的点,如果出现了就说明不能确定大小关系。不论出不出现多个入度为0的点拓扑排序都要进行到最后来判断是否出现环,因为一旦出现环就不用再比了,一定是不能排序的。

另外,注意输出序列之后的那个逗号。。。。。。

Sorting It All Out

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 24   Accepted Submission(s) : 13
Problem 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 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
PKU
#include <iostream>
#include <cstring>
#include <stdio.h>
#define inf 0xfffffff
#define maxn 27 using namespace std; int n,m;
char les,first,second;
int mp[maxn][maxn],ans[maxn]; int topo()
{
int i,j,res=0;
int into[maxn]={0};
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(mp[i][j]>0)
into[j]++;
} }
for(i=0;i<n;i++)
{
int cnt=0,tmp=-1;
for(j=0;j<n;j++)
{
if(into[j]==0)
{
cnt++;
tmp=j;
}
}
if(cnt==0)
return 1;
if(cnt>1)
res=2;
ans[i]=tmp;
into[tmp]=-1;
for(j=0;j<n;j++)
{
if(mp[tmp][j]>0)
into[j]--;
}
}
if(res==2)
return 2;
else
return 3;
} int main()
{
// freopen("in.txt","r",stdin);
while(cin>>n>>m&&(n+m))
{
memset(mp,0,sizeof(mp));
memset(ans,0,sizeof(ans));
int i,tp=2,last=0,num=0;
for(i=0;i<m;i++)
{
cin>>first>>les>>second;
int a=first-'A',b=second-'A';
mp[a][b]=1;
if(tp==2)
{
tp=topo();
}
if(last==2&&(tp==1||tp==3))
{
num=i+1;
}
last=tp;
}
if(tp==3)
{
cout<<"Sorted sequence determined after "<<num<<" relations: ";
for(i=0;i<n;i++)
cout<<char(ans[i]+'A');
cout<<'.';
cout<<endl;
}
else if(tp==2)
cout<<"Sorted sequence cannot be determined."<<endl;
else if(tp==1)
cout<<"Inconsistency found after "<<num<<" relations."<<endl; }
return 0;
}

  

三部曲一(数据结构)-1011-Sorting It All Out的更多相关文章

  1. 三部曲一(数据结构)-1024-Eqs

    解方程整数解的题,通过这道题我学会了这种题的一般做法,对于未知数较少.取值范围较小且解为整数的方程,把一半的未知数移到等式的另一边,然后对两边分别枚举,用哈希配对,如果有相同的结果就找到一组解.具体做 ...

  2. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  3. 三部曲一(数据结构)-1020-Ultra-QuickSort

    通过这道题我大体理解了树状数组的原理和用法,完全用的别人的算法,我把别人算法看懂之后有自己敲了一遍,不得不说这算法真是高深巧妙啊,我开始看都看不懂,还是在别人的讲解下才看懂的,我觉得有必要写个博客记录 ...

  4. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  5. COJ 1011 WZJ的数据结构(十一)树上k大

    题解:主席树&DFS序. PS:为什么我一开始Wa了N发 是因为有一个左区间我写成[L,M+1]了.......................... #include<iostream ...

  6. Python数据结构应用5——排序(Sorting)

    在具体算法之前,首先来看一下排序算法衡量的标准: 比较:比较两个数的大小的次数所花费的时间. 交换:当发现某个数不在适当的位置时,将其交换到合适位置花费的时间. 冒泡排序(Bubble Sort) 这 ...

  7. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

  8. 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  9. 用python语言讲解数据结构与算法

    写在前面的话:关于数据结构与算法讲解的书籍很多,但是用python语言去实现的不是很多,最近有幸看到一本这样的书籍,由Brad Miller and David Ranum编写的<Problem ...

随机推荐

  1. asp.net 之

    <script type="text/javascript"> //获取客户端实例 var pa = Sys.WebForms.PageRequestManager.g ...

  2. linux 用户态 内核态

    http://blog.chinaunix.net/uid-1829236-id-3182279.html 究竟什么是用户态,什么是内核态,这两个基本概念以前一直理解得不是很清楚,根本原因个人觉得是在 ...

  3. easyui datagrid 行右键生成 动态获取(toolbar) 按钮

    var createGridRowContextMenu = function(e, rowIndex, rowData) { e.preventDefault(); var grid = $(thi ...

  4. js替换字符串问题

    利用正则表达式配合replace替换指定字符. 语法 stringObject.replace(regexp,replacement) 参数 描述 regexp 必需.规定了要替换的模式的 RegEx ...

  5. Java-String类的常用方法总结

    一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...

  6. 12.NFS搭建配置

    参考博客:http://www.cnblogs.com/mchina/archive/2013/01/03/2840040.html 1.关闭防火墙和SELINUX $ service iptable ...

  7. Trasformation中的公式报错误,错误数据行定位

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. 凯撒密码加密C语言简单实现

    凯撒加密(Julius Caesar)该方法把一条消息中的每个字母用字母表中固定距离之后的那个字母代替.(如果超越了字母Z,会绕道字母表的起始位置.例如,如果每个字母都用字母表中两个位置之后的字母代替 ...

  9. 使用nodewebx进行前后端开发环境分离

    下载nodewebx(windows环境) npm install nodewebx npm install inherits 为什么要下载inherits,因为nodewebx依赖它... 构建目录 ...

  10. cocos2dx 动画显示异常

    最近遇到一个问题 好多cocostudio导出的动画 显示都会有异常 很明显的融合方式 把混合方式里面的 src 改成one dst 改成 one-src alpha 解决 后面附上同行的文章 浅显易 ...