您的位置:首页 > 大数据 > 人工智能

ZOJ 1259 Rails

2014-07-23 01:13 381 查看
stack的应用

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int goal[1005];
int d[1005];
stack<int>s;
int main()
{
freopen("input.txt","r",stdin);
int n;
for(int i = 0; i <= 1000; i++)
d[i] = i;
while(cin>>n && n)
{
while(cin>>goal[1] && goal[1])
{
while(!s.empty())
s.pop();
for(int i = 2; i <= n; i++)
cin>>goal[i];
int i = 2;
int pos = 1;
s.push(d[1]);
while(pos <= n && i <= n+1)
{
if(s.empty() || s.top() != goal[pos])
{
s.push(d[i]);
i++;
}
else if(s.top() == goal[pos])
{
s.pop();
pos++;
}
else break;
}
if(s.empty())
cout<<"Yes"<<endl;
else cout<<"No"<<endl;

}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: