HDU1022(Train Problem I)

本文介绍了一种使用栈数据结构模拟火车站列车进出站的问题解决方法。针对最多9列火车,需判断是否能通过栈操作使进站顺序变为指定出站顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

[img]http://dl.iteye.com/upload/attachment/569588/891f7ebe-ad2a-369d-a20b-127e2d1684f5.jpg[/img]

Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.


Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.


Sample Input

3 123 321
3 123 312

Sample Output

Yes.
in
in
in
out
out
out
FINISH
No.
FINISH


#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

#define STACK_INTI_SIZE 100
#define STACK_INCREMENT 10

typedef int Status;
typedef char SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

Status initStack(SqStack *s)
{
s->base = (SElemType *)malloc(STACK_INTI_SIZE * sizeof(SElemType));

if(!s->base)
{
exit(OVERFLOW);
}

s->top = s->base;

s->stacksize = STACK_INTI_SIZE;

return OK;
}

Status destoryStack(SqStack *s)
{
free(s->base);
s->base = NULL;
s->top = NULL;
s->stacksize = 0;
return OK;
}

Status clearStack(SqStack *s)
{
s->top = s->base;
s->stacksize = STACK_INTI_SIZE;
return OK;
}

Status getTop(SqStack *s,SElemType *e)
{
if(s->top == s->base)
{
return ERROR;
}

*e = *(s->top - 1);

return OK;
}

Status push(SqStack *s,SElemType e)
{
if(s->top - s->base >= s->stacksize)
{
s->base = (SElemType *)realloc(s->base,(s->stacksize + STACK_INCREMENT) * sizeof(SElemType));

if(!s->base)
{
exit(OVERFLOW);
}

s->top = s->base + s->stacksize;

s->stacksize += STACK_INCREMENT;
}

*s->top = e;

s->top ++;

return OK;
}

Status pop(SqStack *s,SElemType *e)
{
if(s->top == s->base)
{
return ERROR;
}

s->top --;

*e = *s->top;

return OK;
}

int isEmpty(SqStack s)
{
if(s.top == s.base)
{
return 1;
}else
{
return 0;
}
}

int main()
{
SqStack s;
int n;
initStack(&s);
while(scanf("%d",&n) != EOF)
{
char begin[9];
char end[9];
int inout[18];
int len,i,j;

scanf("%s",begin);
scanf("%s",end);
len = strlen(begin);

//声明一个j来单独记录火车出站下标
for(i = 0,j = 0; i < len; i++)
{
char e;

push(&s,begin[i]);
inout[i+j] = 1;

while(!isEmpty(s))
{
getTop(&s,&e);

if(e == end[j])
{
pop(&s,&e);
inout[i + j + 1] = 0;
j++;
}else
{
break;
}
}
}

if(j == len)
{
printf("Yes.\n");
for(i = 0; i < (len*2); i++)
{
if(inout[i] == 1)
{
printf("in\n");
}else
{
printf("out\n");
}
}
printf("FINISH\n");
}else
{
printf("No.\n");
printf("FINISH\n");
}
clearStack(&s);
}
destoryStack(&s);
return 0;
}


基本思路:自己构造了一个栈,用来模仿火车进出站点操作。
由于要匹配输出序列,基本思路先将输入的第一个值入栈,
然后取栈头和输出序列比较,相同则出栈,否则继续将输入
序列中的值入栈,再次比较,和上面思路基本相同。比较完
如果比较到输出序列的末尾则该序列可由输入序列经过栈操
表示同时记录出入操作,否则失败,算法完毕。
【A股温度计】www.agwdj.com 镜像版程序V1.0说明 •通过数据可视化技术,将复杂的A股市场数据转化为直观的图形界面,帮助投资者快速把握市场脉搏。 【核心功能】 •全景视角:突破信息碎片化局限,快速定位涨跌分布,一眼锁定今日热点板块 •板块排序:基于申万行业分类标准,对31个一级行业和131个二级行业实时动态排序 •硬件适配:智能适配不同分辨率屏幕,4K以上屏幕显示信息更多(视觉更佳) •智能缩放:A股全图让大A市场5000+个股同屏显示(支持鼠标滚轮及触摸设备5级缩放) 【三秒原则】 •三秒看懂:通过精心设计的视觉图形,让用户在三秒内看清市场整体状况 •三秒定位:智能算法让大成交额个股和热点板块自动靠前,快速定位机会 •三秒操作:极简的界面,让用户减少操作 【使用场景】 •盘前准备:快速了解隔夜市场变化,制定当日策略 •盘中监控:实时跟踪市场动向,及时把握当日机会 •盘后复盘:全面分析当日市场表现,总结经验教训 【适合人群】 •个人用户:快速了解市场整体趋势变化,辅助决策 •专业人员:获取每天市场的数据云图支持研究工作 •金融机构:作为投研系统的可视化补充组件 •财经媒体:制作专业市场分析图表和报道 【市场切换】 •默认加载"A股全图",可切换单独显示的类型如下: •上证A股/深证A股/北证A股/创业板/科创板/ST板块/可转债/ETF 【程序优势】 •运行环境:纯PHP运行(无需安装任何数据库) •数据更新:实时同步→A股温度计→www.agwdj.com •显示优化:自动适配8K/4K/2K/1080P等不同分辨率的屏幕 •设备兼容:对市面上主流的设备及浏览器做了适配(检测到手机/平板/电视等默认Chrome/Firefox/Edge内核过低的情况会自动提示) 【其他说明】 •A股温度计程序演示网址:https://www.agwdj.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值