HPU2017-栈和队列练习

来源:互联网 发布:linux snmp 监控cpu 编辑:程序博客网 时间:2024/05/06 11:54

刚学完就做这种题还是有点吃力,学长讲的贼特么快,跟不上老司机的车速,,,

A - ACboy needs your help again!

 HDU - 1702 


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

44 FIFOIN 1IN 2OUTOUT4 FILOIN 1IN 2OUTOUT5 FIFOIN 1IN 2OUTOUTOUT5 FILOIN 1IN 2OUTIN 3OUT
Sample Output

122112None23


这道题还是比较基础的栈与队列练习,只要再加上字符串的判断就好啦。


#include<stdio.h>#include<string.h>#include<stack>#include<queue>#include<algorithm>using namespace std;int main(){int T;scanf("%d",&T);while(T--){int m;char s[100];scanf("%d%s",&m,s);if(strcmp(s,"FIFO")==0){queue<int > q;while(m--){char a[100];scanf("%s",a);if(strcmp(a,"IN")==0){int n;scanf("%d",&n);q.push(n); }else{if(q.empty())printf("None\n");elseprintf("%d\n",q.front());q.pop() ;}}} else{stack<int >sta;while(m--){char a[100];scanf("%s",a);if(strcmp(a,"IN")==0){int n;scanf("%d",&n);sta.push(n); }else{if(sta.empty())printf("None\n");else{printf("%d\n",sta.top());sta.pop() ;}}}}}return 0;}

B - Train Problem I

 HDU - 1022 

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

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<stdio.h>  #include<string.h>  #include<stack>  #include<algorithm>using namespace std;   int main()  {      int n,i,j,k,flag[20]={-1};      char s1[10],s2[10];      while(~scanf("%d%s %s",&n,s1,s2))      {  stack<char> s;         j=k=0;          for(i=0;i<n;i++)          {              s.push(s1[i]);              flag[k++]=1;              while(!s.empty()&&s.top()==s2[j])              {                  flag[k++]=0;                  s.pop();                j++;              }          }          //for(int z=0;z<2*n;z++)        //printf("flag[%d]=%d  ",z,flag[z]);        //printf("\n");        if(j==n)         {              printf("Yes.\n");              for(i=0;i<k;i++)              {                  if(flag[i]==1)                      printf("in\n");                   else                      printf("out\n");              }              printf("FINISH\n");          }          else              printf("No.\nFINISH\n");      }      return 0;  }  

E - 寻找大富翁

 HDU - 3785


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

Input

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

Output

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

Sample Input

3 12 5 -15 31 2 3 4 50 0
Sample Output

55 4 3

水题,,,,


#include<stdio.h>#include<algorithm>using namespace std;bool cmp(int a,int b){return a>b;}int main(){int n,m;while( ~scanf("%d%d",&n,&m),n+m){int a[100010];for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n,cmp);for(int i=0;i<m;i++)printf("%d%c",a[i],i==m-1?'\n':' ');}return 0;}


F - Rails

 POJ - 1363 

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

51 2 3 4 55 4 1 2 3066 5 4 3 2 100
Sample Output

YesNoYes


#include<stdio.h>#include<stack>#include<algorithm>using namespace std;int main(){int n;while( ~scanf("%d",&n),n){stack<int > sta;int a[1005];while( scanf("%d",&a[0]),a[0] ){for(int i=1;i<n;i++)scanf("%d",&a[i]);//for(int i=1;i<=n;i++)//sta.push(i);int j=0,k=0;for(int i=0;i<n;i++){sta.push(i+1); if(!sta.empty() && sta.top() == a[j]){sta.pop() ;j++;}}  if(j==n) printf("Yes\n"); else printf("No\n"); } printf("\n");}return 0; } 


H - Array

 CodeForces - 300A


Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold:

  1. The product of all numbers in the first set is less than zero ( < 0).
  2. The product of all numbers in the second set is greater than zero ( > 0).
  3. The product of all numbers in the third set is equal to zero.
  4. Each number from the initial array must occur in exactly one set.

Help Vitaly. Divide the given array.

Input

The first line of the input contains integer n (3 ≤ n ≤ 100). The second line contains n space-separated distinct integers a1, a2, ..., an (|ai| ≤ 103) — the array elements.

Output

In the first line print integer n1 (n1 > 0) — the number of elements in the first set. Then print n1 numbers — the elements that got to the first set.

In the next line print integer n2 (n2 > 0) — the number of elements in the second set. Then print n2 numbers — the elements that got to the second set.

In the next line print integer n3 (n3 > 0) — the number of elements in the third set. Then print n3 numbers — the elements that got to the third set.

The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.

Example

Input
3-1 2 0
Output
1 -11 21 0
Input
4-1 -2 -3 0
Output
1 -12 -3 -21 0


#include<stdio.h>#include<algorithm>using namespace std;int main(){int n;while( ~scanf("%d",&n) ){int a[300];for(int i=0;i<n;i++){scanf("%d",&a[i]);}sort(a,a+n);printf("1 %d\n",a[0]);if(a[n-1]>0){printf("1 %d\n",a[n-1]);printf("%d ",n-2);for(int i=1;i<n-1;i++){printf("%d%c",a[i],i==n-2?'\n':' ');}}else{printf("2 %d %d\n",a[1],a[2]);printf("%d ",n-3);for(int i=3;i<n;i++){printf("%d%c",a[i],i==n-1?'\n':' ');}}}return 0;}


I - 0和5

 51Nod - 1433


小K手中有n张牌,每张牌上有一个一位数的数,这个字数不是0就是5。小K从这些牌在抽出任意张(不能抽0张),排成一行这样就组成了一个数。使得这个数尽可能大,而且可以被90整除。

注意:

1.这个数没有前导0,

2.小K不需要使用所有的牌。

Input

每个测试数据输入共2行。 

 第一行给出一个n,表示n张牌。(1<=n<=1000)  

第二行给出n个整数a00,a11,a22,…,an−1n−1 (aii是0或5 ) 表示牌上的数字。

Output

共一行,表示由所给牌组成的可以被90整除的最大的数,如果没有答案则输出”-1”(没有引号)

Sample Input

45 0 5 0
Sample Output

0

# include <cstdio># include <cstring>int main(){    int t, n, a=0, b=0;    scanf("%d",&n);    while(n--)    {        scanf("%d",&t);        if(t==5) ++a;        else++b;    }    if(b==0) printf("-1\n");    else{ if(a<9) printf("0\n");else{    for(int i=0; i<a/9; ++i)        printf("555555555");    for(int i=0; i<b; ++i)        printf("0");    puts("");}}    return 0;}