`
dnstfengtao
  • 浏览: 18791 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

HDU1022(Train Problem I)

 
阅读更多
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.



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;
}


基本思路:自己构造了一个栈,用来模仿火车进出站点操作。
由于要匹配输出序列,基本思路先将输入的第一个值入栈,
然后取栈头和输出序列比较,相同则出栈,否则继续将输入
序列中的值入栈,再次比较,和上面思路基本相同。比较完
如果比较到输出序列的末尾则该序列可由输入序列经过栈操
表示同时记录出入操作,否则失败,算法完毕。
  • 大小: 23 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics