寒假第三周训练——栈队列题目汇总

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42596    Accepted Submission(s): 15953


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 3213 123 312
 

Sample Output
Yes.inininoutoutoutFINISHNo.FINISH
Hint
Hint
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.".
 

Source

题意:输入开始的顺序和结束的顺序,问能不能以类似栈的方式从开始的顺序变为结束的顺序。简单的堆栈,直接用一个stack就可以解决。能的话输出Yes.并输出如何实现,不能的话就输出No. ,每输出一组就输出一个FINISH。

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <stack> //栈的头文件 
using namespace std;
int x[30];

int main()
{
	int n;
	string a,b;
	while(cin >> n)
	{
		stack<char> s; //初始化栈 
		memset(x,-1,sizeof(x));
		cin >> a >> b;
		int j=0,k=0;
		for (int i=0;i<n;i++)
		{
			s.push(a[i]); //进栈 
			x[k++]=0;
			while (!s.empty()&&s.top()==b[j])
			{
				j++;
				s.pop(); //出栈 
				x[k++]=1;
			}
		}
		if (j==n) 
		{
			printf("Yes.\n");
			for (int i=0;i<2*n;i++)
				if (x[i]==0) printf("in\n");
				else printf("out\n");
		}  
		else printf("No.\n");
		printf("FINISH\n");
	}
	return 0;
}
Ferry Loading IV
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions:2065 Accepted: 849

Description

Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.

There is an l-meter-long ferry that crosses the river. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long as it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up cars that are waiting to cross as long as they fit on its deck. The cars are loaded in the order of their arrival; ferry's deck accommodates only one lane of cars. The ferry is initially on the left bank where it broke and it took quite some time to fix it. In the meantime, lines of cars formed on both banks that await to cross the river.

Input

The first line of input contains c, the number of test cases. Each test case begins with l, mm lines follow describing the cars that arrive in this order to be transported. Each line gives the length of a car (in centimeters), and the bank at which the car arrives ("left" or "right").

Output

For each test case, output one line giving the number of times the ferry has to cross the river in order to serve all waiting cars.

Sample Input

4
20 4
380 left
720 left
1340 right
1040 left
15 4 
380 left
720 left
1340 right
1040 left
15 4 
380 left
720 left
1340 left
1040 left
15 4 
380 right
720 right
1340 right
1040 right

Sample Output

3
3
5
6

Source

题意:有一艘渡轮可以从左岸运车到右岸,也可以从右岸到左岸,左右岸的车是排好队的,渡轮有长度上限,只能运有限辆车,问至少运几次可以把左岸车都运到右岸,右岸都运到左岸。由于车是排好队的,可以用队列控制车的进出。

#include <cstdio>
#include <string>
#include <iostream>
#include <queue> //队列的头文件 
using namespace std;
queue<int> l,r;

int main()
{
	int c,len,m,x,t,sum,cnt;
	string s;
	cin >> c;
	while(c--)
	{
		cin >> len >> m;
		cnt=0;
		for (int i=0;i<m;i++)
		{
			cin >> x >> s;
			if (s=="left") l.push(x);
			else r.push(x);
		} 
		t=-1; //用来控制左岸右岸 
		while(!l.empty()||!r.empty())
		{
			sum=len*100;
			if (t==-1) //t=-1时船在左岸
			{
				while(!l.empty()&&sum>=l.front())
				{
					sum-=l.front();
					l.pop();
				}  
			}
			else //t=1是时船在右岸
			{
				while(!r.empty()&&sum>=r.front())
				{
					sum-=r.front();
					r.pop();
				}
				
			}
			t=-t; //转换左右岸 
			cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}

ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10068    Accepted Submission(s): 5047


Problem Description
ACboy was kidnapped!! 
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
 

Input
The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
 

Output
For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.
 

Sample Input
4 4 FIFO IN 1 IN 2 OUT OUT 4 FILO IN 1 IN 2 OUT OUT 5 FIFO IN 1 IN 2 OUT OUT OUT 5 FILO IN 1 IN 2 OUT IN 3 OUT
 

Sample Output
1 2 2 1 1 2 None 2 3
 

Source

题意:简单的模拟栈和队列的题目。

#include <cstdio>
#include <string>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

int main()
{
	int t,m,x; cin >> t;
	string str,str1;
	while(t--)
	{
		queue<int> q;
		stack<int> s;
		cin >> m >> str;
		if (str=="FIFO")
			for (int i=0;i<m;i++)
			{
				cin >> str1;
				if (str1=="IN") {cin >> x; q.push(x);}
				else if (!q.empty())	
					{
						printf("%d\n",q.front()); q.pop();
					}
				else printf("None\n");
			}
		else
			for (int i=0;i<m;i++)
			{
				cin >> str1;
				if (str1=="IN") {cin >> x; s.push(x);}
				else if (!s.empty())	
					{
						printf("%d\n",s.top()); s.pop();
					}
				else printf("None\n");
			}
	}
	return 0;
} 
Web Navigation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions:35328 Accepted: 15812

Description

Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this. 
The following commands need to be supported: 
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored. 
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored. 
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied. 
QUIT: Quit the browser. 
Assume that the browser initially loads the web page at the URL http://www.acm.org/

Input

Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.

Output

For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.

Sample Input

VISIT http://acm.ashland.edu/
VISIT http://acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT

Sample Output

http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored

Source

题意:模拟网页的前进和后退,可以用栈实现,注意要用另一个栈来保存回退后要前进的网页,需要两个栈,注意有一个起始网站,无法操作时输出Ignored。

#include <cstdio>
#include <string>
#include <iostream>
#include <stack>
using namespace std;

int main()
{
	string str,str1;
	stack<string> s1,s2;
	s1.push("http://www.acm.org/"); //初始网页 
	while(cin >> str && str!="QUIT")
	{
		if (str=="VISIT") //将新网页入栈s1
		{
			cin >> str1;
			cout << str1 << endl;
			s1.push(str1);
			while(!s2.empty())
				s2.pop();
		}
		else if (str=="BACK") //回退然后将现在的网页压入栈s2
		{
			if (s1.size()>1)
			{
				s2.push(s1.top());
				s1.pop();
				cout << s1.top() << endl;
			}
			else printf("Ignored\n");
		}
		else if (str=="FORWARD") //从栈s2取出一个压入栈s1 
		{
			if (!s2.empty())
			{
				s1.push(s2.top());
				cout << s2.top() << endl;
				s2.pop();
			}
			else printf("Ignored\n");
		}
	}
	return 0;
}
Rails
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions:36427 Accepted: 14187

Description

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. 

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. 

The last block consists of just one line containing 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

Sample Output

Yes
No

Yes

Source

题意:类似train的题目,也是判断能不能改变顺序,能输出Yes不能输出No。

#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
int a[1005];

int main()
{
	int n; 
	while(cin >> n && n)
	{
		while(cin >> a[0] && a[0])
		{
			stack<int> s;
			for (int i=1;i<n;i++)
				scanf("%d",&a[i]);
			int j=0;
			for (int i=1;i<=n;i++)
			{
				s.push(i);
				while(!s.empty()&&s.top()==a[j])
				{
					j++;
					s.pop();
				}
			}
			if (j==n) printf("Yes\n");
			else printf("No\n");
		}
		printf("\n");
	}
	return 0;
}

posted on 2018-02-26 12:01  Radium_1209  阅读(130)  评论(0)    收藏  举报

导航