算法竞赛入门经典 UVa 514 Rails

来源:互联网 发布:新红楼梦 知乎 编辑:程序博客网 时间:2024/05/18 00:08

好久没有写了,前几天被第五章虐的不成样子,恶补了一下。

这一题是栈比较简单的应用。

此题的参考代码有点错误,参考代码提交是WA,下面的代码是进行修改过的,AC了。

#include<cstdio>#include<stack>using namespace std;const int maxn = 1010;int target[maxn];int main(){int n;while(scanf("%d",&n) == 1){stack<int> s;while (scanf("%d", &target[1])==1)          {              if (!target[1])              {                  printf("\n");                  break;              }          for (int i = 2; i <= n; i++)          {              scanf("%d", &target[i]);          }  int a =1,b =1,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);a++;}else {ok =0;break;}}printf("%s\n",ok ? "Yes" : "No");}}return 0;}