1. 程式人生 > >Train Problem I ( 順序是不是棧操作)

Train Problem I ( 順序是不是棧操作)

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

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.”.
Hint
Hint
思路 ,,判斷一個 順序進棧是不是可能按照某一個順序出棧
看程式碼

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define M  100
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
int a[M];
char in[100];
char out[100];
int main()
{
    int n;
     while(scanf("%d %s %s",&n,in,out)!=EOF)
    {
        int k=0;
        queue<char>Q;
        stack<char>S;   
        for(int i=0;i<n;)
        {
            Q.push(out[i] );
            S.push(in[i++]);
            a[k++]=1;
            while(Q.front()==S.top())
            {
                a[k++]=0;
                Q.pop();
                S.pop();
                if(Q.empty()||S.empty()) break;  //  在pop 棧和佇列之前 都要判定是不是 為空,否則就會 死迴圈
            }
        }

        if(Q.empty()&&S.empty())
        {
            puts("Yes.");
            for(int i=0;i<k;i++)
            printf("%s\n",a[i]?"in":"out");
            puts("FINISH");
        }
        else 
            printf("No.\nFINISH\n");

        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
    }
    return 0;
}

相關推薦

Train Problem I 順序操作

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(becau

HDU 1022 Train Problem I 資料結構 ——

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

1022】Train Problem I 模擬,水題,思維

題幹: 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

Train Problem I 的基本運用

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

杭電1022——Train Problem I的應用

Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to

Train Problem I經典

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24044    Accept

hdu 3183 A Magic Lamp給一個n位的數,從中刪去m個數字,使得剩下的數字組成的數最小順序能變,然後輸出

urn scan eight sca query names efi family 題目 1.題目大意是,給你一個1000位的數,要你刪掉m個為,求結果最小數。 思路:在n個位裏面刪除m個位。也就是找出n-m個位組成最小數 所以在區間 [0, m]裏面找

HDOJ 1023 Train Problem II 卡塔蘭數

stat scrip fine ger title divide color code ostream Problem Description As we all know the Train Problem I, the boss of the Ignatius Tr

編寫一個在1,2,…,9順序能變數字之間插入+或-或什麼都插入,使得計算結果總是100的程式,並輸出所有的可能性。

public class Test5{ public static void main(String[] args){ /* 編寫一個在1,2,…,9(順序不能變)數字之間插入+或-或什麼都不插入, 使得計算結果總是100的程式,並輸出所有的可能性。 例如:1 + 2 + 3

HDU1023——Train Problem II卡特蘭數

=======================================================================卡特蘭數卡特蘭數又稱卡塔蘭數,是組合數學中一個常出現在各種計數問題中出現的數列。由以比利時的數學家歐仁·查理·卡塔蘭 (1814–1894)命名。 卡特蘭公式的應用很廣

用java編寫在1,2,…,9順序能變數字之間插入+或-或什麼都插入,使得計算結果總是100的程式,並輸出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100

今天看到一個題目,編寫一個在1,2,…,9(順序不能變)數字之間插入+或-或什麼都不插入,使得計算結果總是100的程式,並輸出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。 剛開始看到題目的時候一籌莫展,但是題目下一條

HDU 1023 Train Problem II 卡特蘭數

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11019    Accept

HDU.1022 Train Problem I的簡單應用】3.14

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29144    Accept

hdu1022 Train Problem I---模擬

ron std 不能 pty 結束 type blog tac long 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 題目大意: 車的進出站問題,先給出N個火車,再按序列一的方式進站,判斷能否以序列二的方式出站,若

【HDU】1022Train Problem I可行問題

題目大意:給出一個數(車的數目),兩個字串(前一個為現有車的排序,第二個為要組成的車的排序), Sample Input 3 123 321 3 123 312 Sample Output Yes. in in in out out out FINI

HDU 1022 Train Problem I 的應用

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 48177    Accepted

hdu 1022 Train Problem I

Train Problem ITime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3121    Accepted Su

的模擬 Train Problem I

我這一週正式的開始了對資料結構和經典演算法的學習,第一週內容就只有一點,棧的模擬 棧(stack)又名堆疊,它是一種運算受限的線性表。其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向一個棧插入新元素又稱作進棧、入棧或壓棧,它是把新元素放到棧頂元素的上面,

HD 1022 Train Problem I 的運用 比較好理解的程式碼

#include <stdio.h> #include <stdlib.h> #define max 100 char in[max];//儲存進棧序列 char out[max];//儲存出棧序列 char stack[max];//棧 int f

Train Problem I ————和佇列

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(becau