ZOJ 1259_Rails

来源:互联网 发布:大数据开发工程师 知乎 编辑:程序博客网 时间:2024/05/18 13:41

典型的栈题。

大意:火车从A到B要经过中转站,已知进入序列是顺序的即1~n,给定目标序列,看是否能得到。已知火车进站后不能回到A。

 

分析:很明显用到栈。用count的递增变化来模拟进入序列,用target[]来保存目标序列。

 

#include<iostream>#include<stack>using namespace std;int main(){int n,i,count;int target[1000];//目标序列 stack<int> sk;while(cin>>n&&n!=0){while(cin>>target[0]&&target[0]){for(i=1;i<n;i++)cin>>target[i];count=1;//顺序序列 i=0;//目标序列下标 while(i<n){if(count==target[i])//当前号码与目标号码吻合,next {count++;i++;}else{if(!sk.empty()&&sk.top()==target[i])//当前不吻合,看上一个是否吻合 {sk.pop();i++;}else{if(count<=n)//看序列是否还在范围内 sk.push(count++);elsebreak;}}}if(i==n)//正常退出while即序列可得到 cout<<"Yes"<<endl;elsecout<<"No"<<endl;while(!sk.empty())//清空栈,这个是考虑到当序列不可得到时,栈内有残留 sk.pop();}cout<<endl;}return 0;}


 

原创粉丝点击