PAT 1051 Pop Sequence (25 分)

mac2024-02-02  126

#include <cstdio> #include <stack> using namespace std; int ans[1001]; stack<int> st; int main() { int n,m,k; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<k;i++) { while(!st.empty()) st.pop(); for(int j=1;j<=m;j++) scanf("%d",&ans[j]); //current表示当前下标 int current=1; //flag为false表示栈溢出 bool flag=true; for(int j=1;j<=m;j++) { st.push(j); if(st.size()>n) { flag=false; break; } while(!st.empty()&&st.top()==ans[current]) { st.pop(); current++; } } if(st.empty()==true&&flag==true) printf("YES\n"); else printf("NO\n"); } return 0; }
最新回复(0)