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
A
B
26 1
A
輸出樣例
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
我們現(xiàn)在只看樣例:4和6的含義分別是:
4表示表達(dá)式中一共含有4個(gè)字符,也就是說(shuō)我們需要排序的是4個(gè)樣例,6呢,就是需要用戶(hù)輸入6個(gè)表達(dá)式,我們根據(jù)這兩個(gè)數(shù)值和相應(yīng)的表達(dá)式,去推算字符的先后順序
,可能會(huì)有三種結(jié)果,一種當(dāng)然是排序成功,比如第一個(gè)輸入,一種是會(huì)有邏輯沖突,比如第二個(gè)輸入,一種就是條件不足,不能判斷
我的算法思想是這樣的,定義一個(gè)結(jié)構(gòu)體,該結(jié)構(gòu)體有三個(gè)變量,一個(gè)是key表示字符,一個(gè)是value,表示權(quán)重,一個(gè)就是布爾變量,表示該值是否已經(jīng)參加運(yùn)算
1.根據(jù)用戶(hù)輸入初始換結(jié)構(gòu)體數(shù)組變量(chain_head),將相應(yīng)的key值存到結(jié)構(gòu)體當(dāng)中,value則是UNSET,布爾類(lèi)型變量則是false,表示還未參加到運(yùn)算當(dāng)中,
2.接下來(lái),計(jì)算第n個(gè)表達(dá)式(n初始化成0),我們獲取第n個(gè)表達(dá)式的第一個(gè)和第三個(gè)字符,分別獲得該key在結(jié)構(gòu)體數(shù)組中的索引,index_a和index_b,這樣需要比較的就是
chain_head[index_a]和chain_head[index_b]了,首先看下chain_head[index_a].value是否小于chain_head[index_b].value,如果小于,則不進(jìn)行任何操作,繼續(xù)計(jì)算第n+1個(gè)
表達(dá)式,如果chain_head[index_a].value大于chain_head[index_b].value并且,chain_head[index_b].FirstApper=true(index_b的結(jié)構(gòu)體從未參加運(yùn)算),則表示邏輯沖突,停
止循環(huán),輸出結(jié)構(gòu),如果chain_head[index_a].value==chain_head[index_b].value則chain_head[index_b].value=chain_head[index_a].value+1;即可,然后進(jìn)行第n+1個(gè)表達(dá)
式的計(jì)算,繼續(xù)進(jìn)行第二步的運(yùn)算
3.清理現(xiàn)場(chǎng),輸出結(jié)果
源代碼如下:
// Sort_It.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include
using namespace std;
/****************************************************************************************************************
***文件名稱(chēng):Sort_It.cpp
***文件描述:根據(jù)表達(dá)式對(duì)字符進(jìn)行排序
****************************************************************************************************************/
typedef struct {
char key;
int value;
bool FirstApper;
}Key_Value;
Key_Value* chain_head=NULL;
int nCount=0;//需要排序的字符個(gè)數(shù)
int nCur_Count=0;//當(dāng)前已經(jīng)賦值的個(gè)數(shù)
int result=0;
#define DONE 1
#define INCONSIST 2
#define UNENOUGH 3
#define UNSET 1
void operation(char key_a,char key_b);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///函數(shù)名稱(chēng):TestOver
///函數(shù)描述:檢測(cè)根據(jù)當(dāng)前獲得信息,檢測(cè)是否已經(jīng)可以完成排序(value的值疊加,如果等于1,2,3的和則表示排序完成)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TestOver(Key_Value* head)
{
int nSum_1=0,nSum=0;
for(int i=0;i
感谢您访问我们的网站,您可能还对以下资源感兴趣:
日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话
a∨无码视频手机版|黄色一级免费电影大全|国产福利视频导航|91大神在线精品|亚洲成人a片亚洲系列第一页|免费一级性爱大片|AA级黄色一级特黄成人大片|国产一区二区羞羞乱伦|人妻综合视频成人综合导航、|爱爱视频亚洲丝袜内射
国产一级片子色色亚洲网|日a在线播放国产熟妇性爱网|日本欧美成人电影|国产毛毛浓密茂盛AA性视频|日韩2024无码|欧美黄片免费看毛片电影|国产又大又粗又硬|最新国产黄色大片|日本亚洲三级视频|亚洲va欧美ⅴa在线
成人AV一区国内性视频|轮奸超碰精品福利|另类性爱亚洲性爱手机版|在线观看黄片一区|亚洲精品AⅤ一区二|久久亚洲婷婷五月婷a|99热这就是精品|日韩国产无码网址|日本不卡A V一区|99青草国产精品视频无码一区
