Rails题解C++STL实现

There is a famous railway station in PopPush City. Country there is
incredibly hilly. The stationwas built in last century. Unfortunately,
funds were extremely limited that time. It was possible toestablish
only a surface track. Moreover, it turned out that the station could
be only a dead-end one(see picture) and due to lack of available space
it could have only one track.
在这里插入图片描述

The local tradition is that every train arriving from the direction A
continues in the directionB with coaches reorganized in some way.
Assume that the train arriving from the direction A hasN ≤ 1000
coaches numbered in increasing order 1, 2, . . . , N. The chief for
train reorganizations mustknow whether it is possible to marshal
coaches continuing in the direction B so that their order willbe
a1.a2, . . . , aN . Help him and write a program that decides whether
it is possible to get the requiredorder of coaches. You can assume
that single coaches can be disconnected from the train before they
enter the station and that they can move themselves until they are on
the track in the direction B. Youcan also suppose that at any time
there can be located as many coaches as necessary in the station.But
once a coach has entered the station it cannot return to the track in
the direction A and also onceit has left the station in the direction
B it cannot return back to the station.
Input
The input file consists of blocks of lines. Each block except the last describes one train and possiblymore requirements for
its reorganization. In the first line of the block there is the
integer N describedabove. In each of the next lines of the block there
is a permutation of 1, 2, . . . , N. The last line of theblock
contains just ‘0’.The last block consists of just one line containing
‘0’.
Output
The output file contains the lines corresponding to the lines withpermutations in the input file. A lineof the output file
contains ‘Yes’ if it is possible to marshal the coaches in the order
required on thecorresponding line of the input file. Otherwise it
contains ‘No’. In addition, there is one empty line afterthe lines
corresponding to one block of the input file. There is no line in the
output file corresponding to the last “null” block of the input file.
Sample Input
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
Sample Output
Yes
No
(这里是空的一行)
Yes

想必看到这篇博客的一定是有bear来(指已经看过题目了),所以上面的这堆英文的换行啥的我是随便的,见谅哈。
题意解释:
在这里插入图片描述
A这边是起始站,里面的每个火车车厢编号是从左向右的1、2、3…N-1、N(N是每个样例的首行输入,见下图)在这里插入图片描述
然后下面每行的N个数的输入对应的是终点站的车厢排列状态(从左往右),题目就是要求出起始车站的车厢是否能变成终点站那样的排列顺序,已经进入B终点站的车厢不能再退出来。A和B中间的中转站可以实现车厢顺序重组操作,因为这个中转站是一个,如果从A出来的车厢发现不能匹配上B中(还没匹配上的最靠左的)车厢的话,可以入栈,等待可能会出现的时机出栈。
当然了,作为一个栈,只有栈顶的车厢才与资格与终点站的车厢匹配
然后就是这个输入的问题(有点恶心)
首先对N要多组输入,当输入的N为0时才停止输入,并且不换行
然后在输入N后,对于每行N个数的输入,如果第一个输的数为0的话,就要停止该行输入,换行,进行下个N的输入。

然后是AC代码

//#include<bits/stdc++.h> //都行 
#include<iostream>
#include<stack>
#include<queue>
using namespace std;

stack<int>s;//中转站station
queue<int>b;//b站台//FIFO原则,用队列 

void fun(int N)
{
	int i,temp;
	for(i=1;i<=N;i++)//A站台车厢是按顺序递增排的
	{
		s.push(i);//A站台的车厢入栈 
		while(s.size()&&s.top()==b.front())
		{//这里的s.size()必须要写,而且放在&&前面
		//不然如果栈为空但调用s.top()的话编译器不报错,但循环就是不走了,跟见了鬼一样 
			s.pop();//出栈 
			b.pop();//与B的车厢匹配上 
		}
	}
}

int main()
{
	int N,n,temp;
	while(cin>>N)
	{
		if(N==0)
		{
			//cout<<endl;//不要写!!!! 
			break;
		}
		while(cin>>temp)
		{
			while(!s.empty())s.pop();//千万记得清空!! 
        	while(!b.empty())b.pop();//千万记得清空!! 
			if(temp==0)//第一个数据为0的话
			{
				cout<<endl;//直接换行 
				break;
			}
			b.push(temp);//入队,B车站的左侧 
			for(n=2;n<=N;n++)
			{
				cin>>temp;
				b.push(temp);//入队,B车站的左侧
			}
			fun(N);//走函数 
			if(s.size())//栈不为空 
			{
				cout<<"No"<<endl;//未完全匹配 
			}
			else
			{
				cout<<"Yes"<<endl;//完全匹配 
			}
		}
	}
}
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值