寒假第三周训练——栈队列题目汇总
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



题意:输入开始的顺序和结束的顺序,问能不能以类似栈的方式从开始的顺序变为结束的顺序。简单的堆栈,直接用一个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;
}| 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, m. m 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
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!
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
题意:简单的模拟栈和队列的题目。
#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;
} | Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions:35328 | Accepted: 15812 |
Description
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
Output
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;
}| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions:36427 | Accepted: 14187 |
Description

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 last block consists of just one line containing 0.
Output
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) 收藏 举报
浙公网安备 33010602011771号