每次加入一个关系都要进行拓扑排序,不过在排序过程中需要判断是否出现多个入度为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. Unity中Collider和刚体Collider性能对比

    测试方式: 每个对象做大范围正弦移动,创建1000-5000个对象,保证场景分割树的实时更新,并测试帧率 测试脚本: 移动脚本: using UnityEngine; using System.Col ...

  2. pickle与shelve

    pickle Example 写入文件 import pickle integers = [1, 2, 3, 4, 5] with open('pickle-example.p', 'wb') as ...

  3. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...

  4. Oracle软件开发分析

    软件开发的步骤可大致分为: 1.需求分析 2.系统设计 3.编码实现 4.系统测试 5.运行维护 student class     多对一 sno name age gender cid id  c ...

  5. XML学习笔记(三) -- Schema

    标签(空格分隔): 学习笔记 Schema的格式 XML Schema文档是由元素.属性.命名空间和XML文档中的其他节点构成的. XML Schema有两种重要的Schema模型:Microsoft ...

  6. MXNet 学习 (1) --- 最易上手的深度学习开源库 --- 安装及环境搭建

    安装环境:Win 10 专业版 64位 + Visual Studio 2015 Community. 记录下自己在有GPU的环境下安装配置MXNet的过程.该过程直接使用MXNet release ...

  7. iOS开发数据库篇—SQL代码应用示例

    iOS开发数据库篇—SQL代码应用示例 一.使用代码的方式批量添加(导入)数据到数据库中 1.执行SQL语句在数据库中添加一条信息 插入一条数据的sql语句: 点击run执行语句之后,刷新数据 2.在 ...

  8. php : mysql数据库操作类演示

    设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...

  9. jquery validate 在ajax提交表单下的验证方法

    $(function() {  var method='${method }';  if(method == 'edit'){   url="${ctx}/commodity/typeReN ...

  10. 浅谈 js字符串 trim 方法之正则篇

    关于 trim 其实没啥好说的,无非就是去除首位空格,对于现代浏览器来说只是简单的正则 /^\s+|\s+$/ 就可以搞定了.而且支持中文空格   等等.什么 \s 支持 中文空格?是的. 打开 Re ...