uva 514Rails(栈模拟)

xiaoxiao2021-02-28  35

紫书上的,格式不太对,这个格式有点那啥

#include<cstdio> #include<stack> using namespace std; const int maxn=1000+10; int n,target[maxn]; int main() { while(scanf("%d",&n)==1) { stack<int>s; int A=1,B=1; for(int i=1;i<=n;i++) scanf("%d",&target[i]); int ok=1; while(B<=n) { if(A==target[B]) { A++;B++; } else if(!s.empty()&&s.top()==target[B]) { s.pop();B++; } else if(A<=n) s.push(A++); else { ok=0; break; } } printf("%s\n",ok?"Yes":"No"); } return 0; }

这个是可以AC的

#include<stdio.h> #include<stack> using namespace std; stack<int>s; int main() { int n, a; while (scanf("%d", &n),n) { while (scanf("%d", &a), a) { int cnt = 1,i; while (!s.empty())s.pop(); for (i = 1; i < n; i++) { while (cnt<=n&&(s.empty() || s.top() != a))s.push(cnt++); if (s.top()!=a) { printf("No\n"); while (getchar() != '\n'); break; } s.pop(); scanf("%d", &a); } if (i == n)printf("Yes\n"); } printf("\n"); } }
转载请注明原文地址: https://www.6miu.com/read-2622389.html

最新回复(0)