Rails

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish 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 direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, …, N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, …, aN. Help him and write a program that decides whether it is possible to get the required order 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. You can 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 once it has left the station in the direction B it cannot return back to the station.
Rails_第1张图片
Input
The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, …, N. The last line of the block contains just 0.
Output
The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null’’ block of the input.
Sample Input
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

这个题的意思是有很多火车1,2,3,4,5号火车,进站的顺序就是1,2,3,4,5,,,然后给你一个出站顺序,判断一下是不是能以这样的顺序出站,
这个题还有下面那个题,一直都不是很明白的题,拖了很久
学长讲过之后才算是懂了点

#include
#include
#include
#include
using namespace std;

int main()
{
	int n;
	while(cin>>n&&n)
	{
		while(1)
		{
			stack st;
			int aa[1090];
			int po=0;
			cin>>aa[0];
			if(aa[0]==0)//如果输入的数是0,就直接break
			{
				cout<>aa[i];
			for(int i=1;i<=n;i++)
			{
				st.push(i);
				if(!st.empty()&&st.top()==aa[po])//aa[po]是进站的顺序,ss这个栈是出站的顺序
				{
					st.pop();
					po++;
				}
			}
			if(st.empty())cout<<"Yes"<

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

For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can’t let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output “No.”.

#include
#include
#include
using namespace std;

int main()
{
	int n;
	string in,out;
	while(cin>>n>>in>>out)
	{
		bool no[190];
		stackat;
		int k=0;int j=0;
		for(int i=0;i

你可能感兴趣的:(Rails)