zoukankan      html  css  js  c++  java
  • 栈和队列

    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!

    InputThe 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.OutputFor 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
    模拟
     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 #include <string>
     5 #include <string.h>
     6 #include <cstring>
     7 #include <stdio.h>
     8 #define lowbit(x) x&(-x)
     9 #include <queue>
    10 #include  <stack>
    11 using namespace std;
    12 const int maxn=5e5+10;
    13 typedef long long ll;
    14 
    15 char a[5];
    16 int x;
    17 void solve1(){
    18 
    19 }
    20 
    21 int main(){
    22     ios::sync_with_stdio(0);
    23     int n,m;
    24     while(~scanf("%d",&n)){
    25         while(n--){
    26             scanf("%d %s",&m,a);
    27             if(a[2]=='F'){
    28                 queue<int>q;
    29                 while(m--){
    30                     scanf("%s",a);
    31                     if(a[0]=='I'){
    32                         int ans=0;
    33                         scanf("%d",&ans);
    34                         q.push(ans);
    35                     }else{
    36                     if(q.empty())
    37                         printf("None
    ");
    38                     else{
    39                         printf("%d
    ",q.front());
    40                         q.pop();
    41                     }
    42                 }
    43             }
    44         }else{
    45         stack<int>s;
    46         while(m--){
    47           scanf("%s",a);
    48           if(a[0]=='I'){
    49             int ans=0;
    50             scanf("%d",&ans);
    51             s.push(ans);
    52             }else{
    53             if(s.empty()){
    54                 printf("None
    ");
    55                 }else{
    56                 printf("%d
    ",s.top());
    57                 s.pop();
    58                 }
    59             }
    60           }
    61         }
    62       }
    63    }
    64     return 0;
    65 }

    Train Problem I

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


    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 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.".
     
    Author
    Ignatius.L
     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 #include <string>
     5 #include <string.h>
     6 #include <cstring>
     7 #include <stdio.h>
     8 #include <stack>
     9 #define lowbit(x) x&(-x)
    10 
    11 using namespace std;
    12 const int maxn=5e5+10;
    13 typedef long long ll;
    14 
    15 char s1[100];
    16 char s2[100];
    17 int ans[100];
    18 int main(){
    19     ios::sync_with_stdio(0);
    20     int n;
    21     while(~scanf("%d %s %s",&n, s1,s2)){
    22         int j=0,k=0;
    23         stack<int>s;
    24         while(!s.empty())
    25             s.pop();
    26         memset(ans,-1,sizeof(ans));
    27         for(int i=0;i<n;i++){
    28             s.push(s1[i]);
    29             ans[k++]=1;
    30             while(!s.empty()&&s.top()==s2[j]){
    31                 ans[k++]=0;
    32                 s.pop();
    33                 j++;
    34             }
    35         }
    36         if(j==n){
    37             printf("Yes.
    ");
    38             for(int i=0;i<k;i++){
    39                 if(ans[i])
    40                     printf("in
    ");
    41                 else
    42                     printf("out
    ");
    43             }
    44         }else{
    45         printf("No.
    ");
    46         }
    47     printf("FINISH
    ");
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    2019.10.31 答辩回来后;
    2019.10.22
    2019.10.21 上周总结+今天
    2019.10.20 没有人可以阻挡自己努力,男朋友也不可以。少情感,多学习;;多夸夸他肯定他
    2019.10.19 干什么? 不要纠结了于下手什么了 趁还年轻 去做吧!!
    2019.10.16 每天问自己 三遍 或者更多:我收获了什么?我收获了什么?我收获了什么???
    Java 错误:Constructor call must be the first statement in a constructor
    2019.10.14 解决讨好型人格;时间管理划分等级+设定时间上限
    2019.10.14 今天看到的业务大佬的肺腑之言
    放几张吴悠校园行
  • 原文地址:https://www.cnblogs.com/z-712/p/8638669.html
Copyright © 2011-2022 走看看