您的位置:首页 > 其它

寒假刷题—栈和队列

2016-03-04 22:08 239 查看
A - ACboy needs your help again!

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

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

B - Train Problem I

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

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

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.".

C - Rails

Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u

Submit

Status

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

D - Replace To Make Regular Bracket Sequence

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit

Status

Description

You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but
you can't replace it by ) or >.

The following definition of a regular bracket sequence is well-known, so you can be familiar with it.

Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s1 and s2 be a RBS then the strings <s1>s2, {s1}s2, [s1]s2, (s1)s2 are also RBS.

For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.

Determine the least number of replaces to make the string s RBS.

Input

The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106.

Output

If it's impossible to get RBS from s print Impossible.

Otherwise print the least number of replaces needed to get RBS from s.

Sample Input

Input

[<}){}

Output

2

Input

{()}[]

Output

0

Input

]]

Output

Impossible

E - Windows Message Queue

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Description

Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will
do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message
from the message queue.

Input

There's only one test case in the input. Each line is a command, "GET" or "PUT", which means getting message or putting message. If the command is "PUT", there're one string means the message name and two integer means the parameter and priority followed by.
There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.

Output

For each "GET" command, output the command getting from the message queue with the name and parameter in one line. If there's no message in the queue, output "EMPTY QUEUE!". There's no output for "PUT" command.

Sample Input

GET

PUT msg1 10 5

PUT msg2 10 4

GET

GET

GET

Sample Output

EMPTY QUEUE!

msg2 10

msg1 10

EMPTY QUEUE!

F - 看病要排队

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Description

看病要排队这个是地球人都知道的常识。

不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。

现在就请你帮助医院模拟这个看病过程。

Input

输入数据包含多组测试,请处理到文件结束。

每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。

接下来有N行分别表示发生的事件。

一共有两种事件:

1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)

2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)

Output

对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。

诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。

Sample Input

7

IN 1 1

IN 1 2

OUT 1

OUT 2

IN 2 1

OUT 2

OUT 1

2

IN 1 1

OUT 1

Sample Output

2

EMPTY

3

1

1

G - 寻找大富翁 (水题)

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Description

浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁.

Input

输入包含多组测试用例.

每个用例首先包含2个整数n(0<n<=100000)和m(0<m<=10),其中: n为镇上的人数,m为需要找出的大富翁数, 接下来一行输入镇上n个人的财富值.

n和m同时为0时表示输入结束.

Output

请输出乌镇前m个大富翁的财产数,财产多的排前面,如果大富翁不足m个,则全部输出,每组输出占一行.

Sample Input

3 1

2 5 -1

5 3

1 2 3 4 5

0 0

Sample Output

5

5 4 3

代码:

A:

#include<cstdio>

void FILO(int nn)

{

int shu[1005];char ch1[5];

int p=0;

while (nn--)

{

scanf("%s",ch1);

if (ch1[0]=='I')

{

p++;

scanf("%d",&shu[p]);

}

else if (p==0)

printf("None\n");

else

{

printf("%d\n",shu[p]);

p--;

}

}

}

void FIFO(int nn)

{

int shu[1005];char ch1[5];

int i=0,o=0;

while (nn--)

{

scanf("%s",ch1);

if (ch1[0]=='I')

{

scanf("%d",&shu[i]);

i++;

}

else if (o<i)

{

printf("%d\n",shu[o]);

o++;

}

else

printf("None\n");

}

}

int main()

{

int t;scanf("%d",&t);

while (t--)

{

int n;char ch[5];

scanf("%d%s",&n,ch);

if (ch[2]=='L')

FILO(n);

else

FIFO(n);

}

return 0;

}

B:

#include<cstdio>

int in[12],out[12];

int pan(int nn)

{

int p=1,i=0,j=0,c[12];

c[0]=15;

c[p]=in[i];

while (i<nn&&j<nn)

{

if (c[p]==out[j]&&j<nn)

{

while (c[p]==out[j]&&j<nn&&p>0)

{

p--;j++;

}

}

else

{

p++;i++;

c[p]=in[i];

}

}

if (p==0) return 1;

else return 0;

}

void yun(int nn)

{

int c[12];

int p,j,i;

p=1;i=0;j=0;

c[0]=15;

c[p]=in[i];printf("in\n");

while (i<nn&&j<nn)

{

if (c[p]==out[j]&&j<nn)

{

while (c[p]==out[j]&&j<nn&&p>0)

{

p--;j++;

printf("out\n");

}

}

else

{

p++;i++;

c[p]=in[i];

printf("in\n");

}

}

}

int main()

{

int n;

while (~scanf("%d",&n))

{

getchar();

char cha[12],chb[12];

scanf("%s%s",&cha,&chb);

//printf("%s %s 12\n",cha,chb);

for (int i=0;i<n;i++)

{

in[i]=cha[i]-'0';

out[i]=chb[i]-'0';

}

//for (int i=0;i<n;i++)

//printf("%d ",out[i]);

if (pan(n))

{

printf("Yes.\n");

yun(n);

}

else

printf("No.\n");

printf("FINISH\n");

}

return 0;

}

C:

#include<cstdio>

int in[1050],out[1050];

int pan(int nn)

{

int p=1,i=0,j=0,c[1050];

c[0]=1050;

c[p]=in[i];

while (i<nn&&j<nn)

{

if (c[p]==out[j]&&j<nn)

{

while (c[p]==out[j]&&j<nn&&p>0)

{

p--;j++;

}

}

else

{

p++;i++;

c[p]=in[i];

}

}

if (p==0) return 1;

else return 0;

}

int main()

{

int n;

while (~scanf("%d",&n))

{

if (n==0) break;

for (int i=0;i<n;i++)

in[i]=i+1;

while (~scanf("%d",&out[0]))

{

if (out[0]==0) break;

for (int i=1;i<n;i++)

scanf("%d",&out[i]);

if (pan(n))

printf("Yes\n");

else

printf("No\n");

}

printf("\n");

}

return 0;

}

D:

#include<cstdio>

#include<cstring>

char s[1001000];int in[1001000];

int pp(int xx)

{

if (s[xx]=='<'||s[xx]=='{'||s[xx]=='['||s[xx]=='(')

return 0;

else

return 1;

}

int main()

{

scanf("%s",s);

int ll=strlen(s);

//printf("%d\n",ll);

int inin=0,ou=0,q=1;

for (int i=0;i<ll;i++)

{

if (pp(i))

{

if (inin==0)

{

printf("Impossible\n");

q=0;

break;

}

inin--;

if ((s[i]-s[in[inin]])!=1&&(s[i]-s[in[inin]])!=2)

{

ou++;

}//printf("%d %d %d %d\n",i,ou,s[i],s[in[inin]]);

}

else

{

in[inin]=i;inin++;

}

}

if (q&&inin==0) printf("%d\n",ou);

else if (q) printf("Impossible\n");

return 0;

}

E:

#include<cstdio>

#include<cstring>

#include<queue>

#include<algorithm>

using namespace std;

struct nood{

char name[100];

int can,hao,youxianshu;

friend bool operator<(nood xx,nood yy){

if (xx.youxianshu!=yy.youxianshu)

return xx.youxianshu>yy.youxianshu;

else

return xx.hao>yy.hao;

}

}s[6050];

int main()

{

int p=1;

char ch[5];

nood kk;

priority_queue<nood> que;

while (~scanf("%s",ch))

{

if (ch[0]=='P')

{

scanf("%s%d%d",kk.name,&kk.can,&kk.youxianshu);

kk.hao=p;p++;

que.push(kk);

}

else

{

if (que.empty())

printf("EMPTY QUEUE!\n");

else

{

kk=que.top();

printf("%s %d\n",kk.name,kk.can);

que.pop();

}

}

}

return 0;

}

F:

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

struct nood{

int hao,youxianshu;

}s1[2000],s2[2000],s3[2000];

bool cmp(nood xx,nood yy){

if (xx.youxianshu!=yy.youxianshu)

return xx.youxianshu>yy.youxianshu;

else

return xx.hao<yy.hao;

}

int main()

{

int n;

while (~scanf("%d",&n))

{

s1[0].hao=s2[0].hao=s3[0].hao=0;

s1[0].youxianshu=s2[0].youxianshu=s3[0].youxianshu=0;

int t1,t2,t3,p=0;

t1=t2=t3=0;

while (n--)

{

char ch[5];

scanf("%s",ch);

if (ch[0]=='I')

{

int a,b;

scanf("%d%d",&a,&b);

if (a==1)

{

p++;

t1++;

s1[t1].hao=p;

s1[t1].youxianshu=b;

}

else if (a==2)

{

p++;

t2++;

s2[t2].hao=p;

s2[t2].youxianshu=b;

}

else

{

p++;

t3++;

s3[t3].hao=p;

s3[t3].youxianshu=b;

}

}

else

{

int a;

scanf("%d",&a);

if (a==1)

{

if (t1==0)

printf("EMPTY\n");

else

{

sort(s1,s1+t1+1,cmp);

printf("%d\n",s1[0].hao);

nood pp;

pp=s1[0];

s1[0]=s1[t1];

t1--;

}

}

else if (a==2)

{

if (t2==0)

printf("EMPTY\n");

else

{

sort(s2,s2+t2+1,cmp);

printf("%d\n",s2[0].hao);

s2[0]=s2[t2];

t2--;

}

}

else

{

if (t3==0)

printf("EMPTY\n");

else

{

sort(s3,s3+t3+1,cmp);

printf("%d\n",s3[0].hao);

s3[0]=s3[t3];

t3--;

}

}

}

}

}

return 0;

}

G:

#include<cstdio>

#include<algorithm>

using namespace std;

int main()

{

int n,m;

int shu[100100];

while (~scanf("%d%d",&n,&m))

{

if (n==0&&m==0) break;

for (int i=0;i<n;i++)

scanf("%d",&shu[i]);

sort(shu,shu+n);

if (n<m)

{

printf("%d",shu[n-1]);

for (int i=n-2;i>=0;i--)

printf(" %d",shu[i]);

printf("\n");

}

else

{

printf("%d",shu[n-1]);

for (int i=n-2;i>n-1-m;i--)

printf(" %d",shu[i]);

printf("\n");

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: