当前位置:首页 > > 充电吧
[导读]Description An ascending sorted sequence of distinct values is one in which some form of a less-than

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.

输入样例


4 6
A A B C B A 3 2
A B 26 1
A 0 0


输出样例
Sorted sequence determined after 4 relations: ABCD.

Inconsistency found after 2 relations.

Sorted sequence cannot be determined.

我们现在只看样例:4和6的含义分别是:

4表示表达式中一共含有4个字符,也就是说我们需要排序的是4个样例,6呢,就是需要用户输入6个表达式,我们根据这两个数值和相应的表达式,去推算字符的先后顺序

,可能会有三种结果,一种当然是排序成功,比如第一个输入,一种是会有逻辑冲突,比如第二个输入,一种就是条件不足,不能判断

我的算法思想是这样的,定义一个结构体,该结构体有三个变量,一个是key表示字符,一个是value,表示权重,一个就是布尔变量,表示该值是否已经参加运算

1.根据用户输入初始换结构体数组变量(chain_head),将相应的key值存到结构体当中,value则是UNSET,布尔类型变量则是false,表示还未参加到运算当中,

2.接下来,计算第n个表达式(n初始化成0),我们获取第n个表达式的第一个和第三个字符,分别获得该key在结构体数组中的索引,index_a和index_b,这样需要比较的就是

chain_head[index_a]和chain_head[index_b]了,首先看下chain_head[index_a].value是否小于chain_head[index_b].value,如果小于,则不进行任何操作,继续计算第n+1个

表达式,如果chain_head[index_a].value大于chain_head[index_b].value并且,chain_head[index_b].FirstApper=true(index_b的结构体从未参加运算),则表示逻辑冲突,停

止循环,输出结构,如果chain_head[index_a].value==chain_head[index_b].value则chain_head[index_b].value=chain_head[index_a].value+1;即可,然后进行第n+1个表达

式的计算,继续进行第二步的运算

3.清理现场,输出结果

源代码如下:

// Sort_It.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
using namespace std;
/****************************************************************************************************************
***文件名称:Sort_It.cpp
***文件描述:根据表达式对字符进行排序
****************************************************************************************************************/
typedef struct {
	char key;
	int value;	
	bool FirstApper;
}Key_Value;
Key_Value* chain_head=NULL;
int  nCount=0;//需要排序的字符个数
int  nCur_Count=0;//当前已经赋值的个数
int result=0;
#define DONE  1
#define INCONSIST 2
#define UNENOUGH 3
#define UNSET  1
void operation(char key_a,char key_b);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///函数名称:TestOver
///函数描述:检测根据当前获得信息,检测是否已经可以完成排序(value的值叠加,如果等于1,2,3的和则表示排序完成)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TestOver(Key_Value* head)
{
	int nSum_1=0,nSum=0;
	for(int i=0;ichain_head[index_b].value&&(chain_head[index_b].FirstApper==false||chain_head[index_a].FirstApper==false))
	{
		//违反逻辑
		result=INCONSIST;
	}
	else
	{
		//重新赋值
		chain_head[index_b].value=chain_head[index_a].value+1;		
		chain_head[index_b].FirstApper=false;
		chain_head[index_a].FirstApper=false;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///函数结束
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///函数名称:struct_init
///函数名称:根据用户输入将信息转换成我们需要操作的几种结构体
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void struct_init(char**strChar,int nRelationsLen)
{
	//开始向里面赋值
	bool mark=false;
	int nCur=0;
	for(int i=0;i>nCount;
	cin>>nRelationsLen;
	chain_head=new Key_Value[nCount];
	char **strChar=NULL;
	strChar=new char*[nCount];	
	for(int i=0;i>strChar[i];		
	}
	char* str_output=NULL;
	struct_init(strChar,nRelationsLen);
	for(int i=0;i



本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

在工业控制系统中,Modbus RTU协议的CRC校验如同通信网络的"免疫系统",某石化厂DCS系统曾因CRC计算错误导致0.3%的数据包丢失,引发连锁控制故障。本文将深入解析CRC-16/MODBUS算法原理,对比软件...

关键字: Modbus RTU CRC 算法

加密算法分对称加密和非对称算法,其中对称加密算法的加密与解密密钥相同,非对称加密算法的加密密钥与解密密钥不同,此外,还有一类不需要密钥的散列算法。

关键字: 算法 嵌入式

在现代数字系统设计中,将算法高效地转化为 RTL(寄存器传输级)实现是 FPGA 工程师的核心任务之一。这一过程不仅需要对算法有深入理解,还需掌握 FPGA 的硬件特性和设计技巧。本文将详细介绍从算法到 RTL 实现的关...

关键字: 算法 寄存器传输级 数字系统

从本质上讲,算法是一种有条不紊、分步骤解决问题或完成任务的方法。无论是简单的数字相加公式,还是复杂的机器学习协议,算法都是软件应用的基础,确保任务能够高效有效地执行。

关键字: 算法 嵌入式

在自动驾驶技术的发展历程中,激光雷达(LiDAR)宛如一颗备受瞩目的新星,其独特的技术特性使其成为追求高安全性、高可靠性自动驾驶方案的首选。然而,这颗新星并非毫无争议,“价格昂贵、结构复杂、算法难度高” 等标签,也让一些...

关键字: 自动驾驶 激光雷达 算法

4月2日消息,近日,有关智能驾驶而引发的交通事故在网络上引起了大家的热烈讨论,对此,央视网评指出,“智能驾驶”,也请握紧方向盘。

关键字: 算法 智能驾驶

所谓排序算法,即通过特定的算法因式将一组或多组数据按照既定模式进行重新排序。这种新序列遵循着一定的规则,体现出一定的规律,因此,经处理后的数据便于筛选和计算,大大提高了计算效率。对于排序,我们首先要求其具有一定的稳定性,...

关键字: 排序算法 算法

快速排序通过一趟排序将待排序列分割成独立的两部分,其中一部分序列的关键字均比另一部分序列的关键字小,则可分别对这两部分序列继续进行排序,以达到整个序列有序的目的。

关键字: 快速排序 算法

算法,作为解决问题的精确描述,是描述策略机制的系统方法。让我们在周末轻松探讨五个具有深远影响的算法:Metropolis-Hastings算法、单纯形法、快速傅立叶变换、快速排序算法,以及计算特征值的QR算法。这些算法在...

关键字: 算法 快速排序算法

服务需要保护自己,以免被太多的请求淹没(无论是恶意或无意的),从而保持可用性。举个生活中的例子,某个景区,平时可能根本没什么人前往,但是一旦到了国庆假日就人满为患,这时景区管理人员就会实施一系列的限流举措,来限制进入的人...

关键字: 限流 算法
关闭