順序出棧(模擬棧)

2021-10-05 16:56:57 字數 1048 閱讀 5587

順序出棧

description

給出乙個容量為m的棧和乙個1~n的任意排列和,

將n個數按照給定排列壓入棧中,請你求出其是否能

夠以1,2,3…n的方式出棧。

input

第一行給出n和m,其中1<=m<=n<=1000,

第二行給出長度為n的排列。

output

如果給定的排列能以1,2,3…n的方式出棧,輸出yes,

否則輸出no。

sample input 1

7 51 2 3 4 5 6 7

7 51 7 6 5 4 2 3

7 67 6 5 4 3 1 2

7 67 6 5 4 3 2 1

sample output 1

yesyes

yesno

思路:可能是我入戲太深, 棧的出棧順序可能本來就沒搞懂再加上題意沒看懂,才沒做出來吧

本來以為會用到棧混洗(可判斷出棧順序是否合理),但是模擬就能做(能否按指定出棧順序出棧)。

#include

using

namespace std;

queue<

int> s;

//開始的順序

stack<

int> t;

//初始棧

queue<

int> e;

//結果順序

intmain()

int key =1;

while

(!s.

empty()

)}//棧已滿

else

if(t.

size()

== m)

else

else}}

}}int cnt =0;

while

(!e.

empty()

) e.

pop();

}if(cnt != n)

else

}return0;

}

出棧入棧順序問題

不定項選擇題 依次讀入資料元素序列入棧,每進乙個元素,機器可要求下乙個元素入棧或彈棧,如此進行,則棧空時彈出的元素構成的序列是以下 序列?牛客444334號 ada 可行步驟 a入棧,b入棧,c入棧,d入棧,d出棧,e入棧,e出棧,c出棧,f入棧,f出棧,b出棧,g入棧,g出棧,a出棧 b 不可行,...

入棧出棧的順序問題

題意 某個字母序列,把這字母序列按順序壓入棧中,在任意過程,允許字元出棧,求所有的可能性 思路 模擬出棧入棧的過程,暴力列舉每一種情況。include include include include include using namespace std int n char str 1000 vo...

python列表模擬棧 壓棧,出棧,查詢

root kl bin python3 author kl 列表模擬棧 壓棧,出棧,查詢 flist 定義棧 defpush it 定義壓棧 data input 資料 strip 提示使用者輸入資料,strip去掉兩邊空格 if data 判斷使用者輸入資料是否為空 else print 輸入內容...