Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.
5 7 5 1 2 3 4 5 6 7 3 2 1 7 5 6 4 7 6 5 4 3 2 1 5 6 4 3 7 2 1 1 7 6 5 4 3 2
YES NO NO YES NO
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
int main(){
int m,n,k,num;
cin>>m>>n>>k;
while(k--){
stack<int> s;
vector<int> v;
for(int i=1;i<=n;i++){ //先输入给出的出栈序列
cin>>num;
v.push_back(num);
}
int current=0;
bool flag=true; //标记出栈序列是否合法
for(int i=1;i<=n;i++){ //按序入栈
s.push(i);
if(s.size()>m){ //此时栈中元素大于栈容量,说明出栈序列非法
flag=false;
break;
}
while(!s.empty()&&s.top()==v[current]){ //栈顶元素与出栈序列当前位置元素相同
s.pop();
current++; //出栈序列指针后移
}
}
if(s.empty()&&flag==true) cout<<"YES"<<endl; //在所有元素入栈后却不能全部出栈,出栈序列肯定非法
else cout<<"NO"<<endl;
}
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
int Solve(int *a, int m, int n)
{ int p = 0; stack<int> s; for(int i=0;i<n;i++) { if(a[i]>p) { for(int j=p+1;j<=a[i];j++) { s.push(j); if(s.size() > m) { cout<<"NO"<<endl; return 0; } } p = a[i]; } if(s.empty()) { cout<<"NO"<<endl; return 0; } int x = s.top(); s.pop(); if(x != a[i]) { cout<<"NO"<<endl; return 0; } } cout<<"YES"<<endl; return 0; }
int main()
{ int n,m,k; cin>>m>>n>>k; int a[n]; while(k--) { for(int i=0;i<n;i++) cin>>a[i]; Solve(a, m, n); } return 0;
}
//PAT A1040
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
//核心思维是模拟出栈(也包括入栈)的过程
//算法一开始,序列首元素以及其之前的元素从小到大依次入栈,再把栈顶出栈,形成初始状态,
//然后考查序列下一元素,分以下情况:
//某一时刻,如果已出栈的最大元素小于出栈序列当前值,则若要序列正确,必有介于这两者之间的元素依然在栈中
//故将尚未push的元素从小到大push,直到栈顶值等于出栈序列当前值,进入下一种情况;若这个过程中栈满则序列错误;
//某一时刻,如果已出栈的最大元素大于出栈序列当前值,则满足序列正确的必要不充分条件,考查下列情况
//某一时刻,如果栈顶值等于出栈序列当前值,则直接做一次pop,表示序列目前为止没有错;
//某一时刻,如果栈顶值不等于出栈序列当前值,则序列错误;
int m, n, k;
vector<int> input[1005];
stack<int> S;//须注意到,已出栈序列的顺序可能很混乱,但是仍在栈中的元素必然是从小到大排列!
bool keepPushing(int first, int last)
{
for (int i = first; i <= last; ++i) {
if ((int)S.size() < m)S.push(i);
else return false;//若序列在入栈过程中超出容量,则错误
}
return true;
}
bool check(int line)
{
keepPushing(1, input[line][0]);
int cur, beCheck = S.top();//beCheck标记已出栈的最大元素
S.pop();//构造初始状态
for (int j = 1; j < n; ++j) {//考查序列[1,n)各个元素
cur = input[line][j];
if (beCheck < cur) { keepPushing(beCheck + 1, cur); beCheck = S.top(); }
if (S.top() == cur) S.pop(); else return false;
}
return true;
}
int main()
{
cin >> m >> n >> k;
int temp;
for (int i = 0; i < k; ++i) { for (int j = 0; j < n; ++j) { cin >> temp; input[i].push_back(temp); } }
for (int i = 0; i < k; ++i) {
check(i) ? (cout << "YES" << endl) : (cout << "NO" << endl);
while (!S.empty())S.pop();
}//每次考查一行之后需要清空栈S
return 0;
}
#include<stdio.h>
int main()
{
int M,N,K,a[1001],i,j,l;
char c[2];
scanf("%d %d %d",&M,&N,&K);//M为栈长,N为数组长,K为组数
while(K--){
for(i=j=l=0;i<N;i++){//l为当前栈长,j为最近入栈的数
scanf("%d",a);
while(l<M&&a[0]>j)a[++l]=++j;
if((l==M&&a[0]>j)||a[l--]<a[0])i=N;
}
gets(c);
printf(i>N?"NO\n":"YES\n");
}
} #include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
int main() {
int m, n, k;
scanf("%d %d %d", &m, &n, &k);
stack<int> num_stack;
for(int i=0; i<k; i++) {
int temp =1;
bool flag = true;
for(int j=0; j<n; j++) {
int number;
scanf("%d", &number);
if(!num_stack.empty() && number == num_stack.top()) {
num_stack.pop();
} else if(number == temp) {
if(num_stack.size() == m)
flag = false;
temp++;
} else if(number > temp){
int cnt = num_stack.size();
while(temp<=number && cnt<=m) {
num_stack.push(temp);
temp++;
cnt++;
}
num_stack.pop();
temp = number + 1;
if(cnt>m)
flag = false;
}else{
flag = false;
}
}
while(!num_stack.empty()){
num_stack.pop();
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
}
思路: 模拟入栈的过程
比如 3 2 1 7 5 6 4
3 比 输入的1大 那么在栈中最少要压入1 2 3,然后把 3 弹出。
用一个数组记录哪些数字已经被压入过了
如果其中栈的大小超过了 应该的大小直接判断 不能够
然后再整体对压入的数据进行判断。
#include <iostream>
#include <stack>
#include <fstream>
#include <vector>
using namespace std;
#ifndef debug
ifstream ifile("case.txt");
#define cin ifile
#endif
bool Check(vector<int> &in, vector<bool> &v, int staSiz, int n)
{
stack<int> sta;
bool check = false;
vector<int> tmp;
for (int i = 0; i < n; i++)
{
if (in[i] >= i+1)
{
for (int j = 0; j < in[i]; j++)
{
if(!v[j])
sta.push(j + 1);
v[j] = true;
}
if (sta.size() > staSiz)
{
return false;
}
}
tmp.push_back(sta.top());
sta.pop();
}
for (int i = 0; i < n; i++)
{
if (tmp[i] != in[i])
{
return false;
}
}
return true;
}
int main()
{
int n;
int staSiz;
int k;
cin >> staSiz >> n >> k;
for (int i = 0; i < k; i++)
{
vector<int> in(n);
vector<bool> v(n, false);
for (int i = 0; i < n; i++)
{
cin >> in[i];
}
bool check = Check(in, v, staSiz, n);
if (check)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
system("pause");
}
#include <iostream>
#include <malloc.h>
using namespace std;
typedef int ElemType;
typedef int Position;
const ElemType ERROR = -1;
struct SNode{
ElemType * Data;
Position top;
int MaxSize;
};
typedef struct SNode * Stack;
Stack Create( int MaxSize )
{
Stack s = (Stack)malloc(sizeof(struct SNode));
s -> Data = (ElemType *)malloc(MaxSize * sizeof(ElemType));
s -> top = -1;
s -> MaxSize = MaxSize;
return s;
}
bool isFull( Stack s )
{
return ( s -> MaxSize - 1 == s -> top );
return false;
}
bool isEmpty( Stack s )
{
return ( -1 == s -> top );
}
bool push( Stack s, ElemType e )
{
if ( isFull(s) ) return false;
s -> Data[++(s -> top)] = e;
return true;
}
ElemType pop( Stack s )
{
if ( isEmpty(s) ) return ERROR;
return s -> Data[(s -> top)--];
}
int main()
{
// freopen("F://input.txt","r",stdin);
int M,N,K;
cin >> M >> N >> K;
while (K) {
Stack s;
s = Create(M);
int ptr;
cin >> ptr;
int check = N;
int start = 1;
s -> Data[s -> top] = -1;
while (check) {
// 栈顶元素和当前输入的元素相等
if ( s -> Data[s -> top] == ptr ) {
ElemType temp = pop(s);
// start控制下一次批量放入堆栈的起点
start = (ptr >= start? ptr + 1 : start);
check--;
// 如果一整行都check完毕了,那么就防止把下一行的输入在这一行
if ( check ) cin >> ptr;
}
// 栈顶元素和当前元素不等
else if ( s -> Data[s -> top] != ptr ) {
// 从check完毕的起始点开始到当前值全部入栈
for ( int i = start; i <= ptr; i++ ) push(s,i);
}
// 一种情况是栈满了但是当前值并不等于栈顶元素,即再也放不进去了
if ( isFull(s) && ptr != s -> Data[s -> top] ) break;
// 第二种情况是栈顶元素比当前值还要大,按照逻辑当前值必定先于其输出,不合逻辑
if ( ptr < s -> Data[s -> top] ) break;
}
// 注意不要忘了把当前行所有值读入完毕
for ( int i = 1; i < check; i++ ) cin >> start;
cout << (check? "NO" : "YES") << endl;
K--;
}
return 0;
}
#include <stdio.h>
#include <stack>
using namespace std;
stack<int> st;
int main(void){
int M, N, K; //M£ºÕ»×î´óÈÝÁ¿£¬N£»ÊäÈë&Êä³öÐòÁеij¤¶È£¬K£»Òª¼ì²éµÄÊä³öÐòÁиöÊý
scanf("%d%d%d", &M, &N, &K);
int output[1005]; //´æ¼ì²éµÄÊä³öÐòÁУ¬³¤¶È±ØÈ»µÈÓÚÊäÈëÐòÁÐ
while(K--){ //KÌõÐòÁÐ
// bool flag = 1; //ÅжÏÊÇ·ñÕýÈ·µÄ±ê־λ
int index = 1; //Êä³öÐòÁеÄϱê
while(!st.empty()) { //Çå¿ÕÕ»
st.pop();
}
for(int i = 1; i <= N; i++){ //дÈë´ý¼ì²éµÄÊä³öÐòÁÐ
scanf("%d", &output[i]);
}
// printf("%d\n", N);
for(int j = 1; j <= N; j++){ //ÊäÈëÐòÁÐΪ 1,2,3,4,5...N
st.push(j); //ÏÈÒÀ´ÎÈëÕ»
// printf("%d\n", j);
if(st.size() > M){
// flag = 0;
break;
}
while(!st.empty() && st.top() == output[index]){
st.pop(); //Êä³ö
index++;
}
}
if(st.empty() && index == N + 1) printf("YES\n");
else printf("NO\n");
}
return 0;
}
#include <stdio.h>
#include <stack>
using namespace std;
stack<int> st;
int pop_str[1100];
void clear_stack()
{
while(!st.empty())
{
st.pop();
}
}
int main()
{
#if 0
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int n,stack_max,m,i;
scanf("%d %d %d",&stack_max,&m,&n);
while(n--)
{
for(i=0;i<m;i++)
{
scanf("%d",&(pop_str[i]));
}
clear_stack();
int current = 0;
bool flag = true;
for(i=1;i<=m;i++)
{
st.push(i);
if(st.size() > stack_max)
{
flag = false;
break;
}
//printf("top=%d popstr=%d\n",st.top(),pop_str[current]);
while(!st.empty() && st.top() == pop_str[current]) //key processing
{
st.pop();
current++;
}
}
//printf("current=%d\n",current);
if(flag == true && current == m)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
#include <iostream>
using namespace std;
int stack[1001], M, N, K, top = 0;
inline void clear() { top = 0; }
inline bool push(int value) {
if (top == M)return false;
stack[top++] = value;
return true;
}
inline void pop() { top--; }
inline bool peek(int &value){
if (!top)return false;
value = stack[top - 1];
return true;
}
int main(int argc, const char* argv[]) {
ios::sync_with_stdio(false);
cin >> M >> N >> K;
int read, cmp;
while (K--) {
bool jumpOut = false, next = true;
// jump是判断NO时跳出循环用 next是正确弹出一个数字后更新i用
int num = 1; // 如果要压,压进去的数字
for (int i = 0;i < N;) { // i代表现在在判断第i+1个输入能否被正确弹出
if(next) {
cin >> read;
i++;
next = false;
}
if (peek(cmp)) {
if (cmp == read) {
pop();
next = true;
continue;
}
else if (cmp > read || !push(num++))jumpOut = true;
}
else push(num++); // 空栈就压一个数进去
if (jumpOut) {
while (i++ < N)cin >> read; // 读掉剩余
cout << "NO" << endl;
break;
}
}
if (!jumpOut)cout << "YES" << endl;
clear();
}
//system("pause");
return 0;
}
// 最后一个输入时候,如果空栈会有点小问题,不过如果输入数据不重复不越界那肯定是对的
// 所以在输出yes后clear一下就没问题了
#include <iostream>
#include <cstring>
#include <stack>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int sequence[1001];
bool exist[1001];
int main(int argc, char** argv) {
int M, N, K;
bool valid;
cin >> M >> N >> K;
for(int i = 0;i < K;i++){
valid = true;
memset(exist, false, sizeof(exist));
stack<int> stk;
for(int j = 0;j < N;j++){
cin >> sequence[j];
}
for(int j = 0;j < N;j++){
if(stk.empty() || stk.top() < sequence[j]){
for(int k = 1;k <= sequence[j];k++){
if(!exist[k]){
stk.push(k);
exist[k] = true;
}
}
if(stk.size() > M){
valid = false;
break;
}
stk.pop();
}
else if(stk.top() == sequence[j]){
stk.pop();
}
else if(stk.top() > sequence[j]){
valid = false;
break;
}
}
if(valid){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int Max=1010;
int a[Max];
stack<int> s;
int main() {
ios::sync_with_stdio(0);
int m,n,k;
cin>>m>>n>>k;
while(k--) {
while(!s.empty()) {
s.pop();
}
for(int i=1;i<=n;i++){
cin>>a[i];
}
int current=1;
bool flag=1;
for(int i=1; i<=n; i++) {
s.push(i);
if(s.size()>m) {
flag=0;
break;
}
while(!s.empty()&&s.top()==a[current]) {
s.pop();
current++;
}
}
if(s.empty()&&flag==1) {
cout<<"YES"<<endl;
} else {
cout<<"NO"<<endl;
}
}
return 0;
} #include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < k; i++)
{
int x = 1, y = 1, src[1009],ok=1;
stack<int> s;
for (int j = 0; j < m; j++) {
cin >> src[j+1];
}
while (x<=m)
{
/*cout << src[x] << " " << y << " ";
if (s.size() != 0) cout << s.top()<<" "<<s.size();
cout<< endl;*/
if (y == src[x]) { x++; y++; }
else if (!s.empty() && s.top() == src[x]) { s.pop(); x++; }
else if (y <= m && s.size()<= n)s.push(y++);
else { ok = 0; break; }
}
if (!ok) {
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
}
}
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
const int N=100010;
int m,n,k;
int a[N];
int main()
{
cin>>m>>n>>k;
for(int i=0;i<k;i++)
{
bool flage=1;
stack<int>st;
for(int j=0;j<n;j++)
scanf("%d",&a[j]);
st.push(1);
int tt=0,t=2;
int top;
while(1)
{
if(st.size()>m)
{
flage=0;
break;
}
top=st.top();
if(a[tt]>top)st.push(t++);
else
{
if(st.top()==a[tt])
{
st.pop();
if(st.size()==0)st.push(t++);//空的话必须入栈
tt++;
if(tt==n)break;
}
else
{
flage=0;
break;
}
}
}
if(flage)puts("YES");
else puts("NO");
}
return 0;
} #include<iostream>
(720)#include<vector>
#include<stack>
using namespace std;
int main(){
int M,N,K,x;
cin>>M>>N>>K;
while(K--){
bool flag=true;
int index=1;
stack<int>myStack;
for(int i=0;i<N;i++){
cin>>x;//5 6 4 3 7 2 1
if(myStack.empty()||myStack.top()<x){
for(;index<=x&&myStack.size()<M;index++){//<=x就不用管序列的大小了
myStack.push(index);
}
}//3 2 1 7 5 6 4
if(myStack.top()!=x){
flag=false;
}
else{
myStack.pop();
}
}
if(flag){printf("YES\n");}
else{printf("NO\n");}
}
} #include<stdio.h>
int main() {
int pop[1003]={0};
int pop1[1003]={0};
int top=-1;
int m,k,n;
scanf("%d %d %d",&m,&n,&k);
for(int p=0; p<k; p++) {
top=-1;
for(int i=0; i<n; i++) {
scanf("%d",&pop1[i]);
}
int w=1,q=0;
while(top<m-1&&w<=n) {
pop[++top]=w++;
while(pop1[q]==pop[top]) {
if(pop1[q++]!=pop[top--]) {
q--;
top++;
break;
}
}
}
if(q==n) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
a = list(map(int,input().split()))
for i in range(a[2]):
m,n,p = list(range(1,a[1] + 1)),[],0
for j in list(map(int,input().split())):
if j not in n:
n += m[p:j - 1]
if len(n) >= a[0]:
print("NO")
break
p = j
elif j == n[-1]:
n = n[:-1]
else:
print("NO")
break
else:
print("YES")
#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;
typedef stack<int> istack;
const int maxn = 1009;
int output[maxn];
istack temp;
//clear all element in temp
void clear(){
while (!temp.empty()) temp.pop();
}
int main(){
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
while (q--){
//get input data
for (int i = 0; i < m; i++) scanf("%d", &output[i]);
//simulate each input
bool have = true; //can find or not
int input = 1; //the value shold output next
clear();
for (int i = 0; i< m ; i++){
//directly pop from input
if(output[i] == input ){
input++;
continue;
}
//check if can pop from the stack
if (!temp.empty() && temp.top() == output[i]){ //you should check if it is empty before use pop method
temp.pop();
continue;
}
//push into stack if it can
while (output[i] != input && temp.size()<n-1 && input<=m ){ //注意size要小于n-1,而不是n
temp.push(input++);
}
if ( output[i] != input){
have = false;
break;
}
else if(input<m){
input++;
}
}
if (have) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
} using namespace std;
int m, n,k, index,temp;
bool flag;
int main()
{
cin >> m >> n >> k;
for (int i = 0; i < k; i++) {
index = 1;
flag = 1;
stack<int>s;
for (int j = 0; j < n; j++) {
cin >> temp;
while (s.empty() || s.top() != temp) {
s.push(index);
if (index > n || s.size() > m) {
flag = 0; break;
}
index++;
}
s.pop();
}
if (flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}
/** 简单概括就是先进行添加栈在出栈
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class Main
{
static int pop_num;
static Stack<Integer> stack = new Stack<>();
public static void main(String[] args) throws IOException
{
Scanner in=new Scanner(System.in);
pop_num=in.nextInt();
int all_num=in.nextInt();
int line_num=in.nextInt();
int [] temp=new int[all_num];
for(int j=0;j<line_num;j++)
{
for (int i = 0; i < all_num; i++) {
temp[i] = in.nextInt();
}
if (pop(all_num, temp) == true) {
System.out.println("YES");
} else if (pop(all_num, temp) == false) {
System.out.println("NO");
}
}
}
public static boolean pop(int N,int [] line)
{
int now_pop=0,i=1,j=0,temp;
while (j<N)
{
temp=line[j];
if(now_pop>0&&stack.peek()==temp )
{
stack.pop();
j++;
now_pop--;
continue;
}
while (i <= N)
{
if(now_pop<pop_num)
{
stack.push(i);
now_pop++;
i++;
break;
}
if(now_pop>=pop_num)
{
return false;
}
}
if(i>N&&stack.peek()!=temp )
{
return false;
}
}
return true;
}
}
解题思路:
对于每行要测试的数据,单独进行模拟验证,符合要求输出YES,否则输出NO。
再验证流程:
再弹出栈顶原素和待检测数值比较时,需要判断栈顶是否为空。
3. 重复操作 2 即可
在比较过程中,发现不符号要求即可终止判断。
代码如下:
import java.io.PrintStream; import java.util.Scanner; import java.util.Stack; public class Main { public static PrintStream out = System.out; public static Scanner in = new Scanner(System.in); public void popSequenceTest(String[] seq, int n, int capacity) { int[] array = new int[n]; for(int i=0;i<n;++i){ array[i] = Integer.valueOf(seq[i]); } Stack<Integer> stack = new Stack<>(); int i,j,index = 0; for(i=0;i<n;++i){ // 需要入栈 if(array[i]>index){ for(j=index+1;j<=array[i];++j){ stack.push(j); // 栈溢出 if(stack.size()>capacity){ out.println("NO"); return; } } index = array[i]; } // 出栈异常 if(stack.empty()){ out.println("NO"); return; } // 出栈正常 int val = stack.pop(); if(val != array[i]){ // 输出的待验证的序列错误 out.println("NO"); return; } } out.println("YES"); } public void test() { int M, N, K; M = in.nextInt(); N = in.nextInt(); K = in.nextInt(); in.nextLine(); // 读取空白行 for (int i = 1; i <= K; ++i) { String[] seq = in.nextLine().split(" "); popSequenceTest(seq, N, M); } } public static void main(String[] args) { Main m = new Main(); m.test(); } }