13 solutions
-
0
写的有点复杂,开了三个栈
#include<bits/stdc++.h> using namespace std; stack <int> a; stack <int> b; stack <int> c; int main(){ int n,en[1005],be[1005]; cin>>n; for(int i=1;i<=n;i++){ be[i]=i;//将开始的火车存入be数组 } for(int i=1;i<=n;i++){ cin>>en[i]; a.push(en[i]);//将最终的火车压入栈a } int j=1;//查找开始火车,一节一节查找 for(int i=1;i<=n;i++){ if(en[j]==be[i]){//相等直接压入 b.push(be[i]); j++; } else if(!c.empty()&&en[j]==c.top()){//查找是否与c站压顶相等 b.push(c.top()); //一定要判断c栈为空,害我改了一小时,我是fw c.pop(); j++; i--; } else{ c.push(be[i]); } } while(!c.empty()){//将c栈中的数压入b栈 b.push(c.top()); c.pop(); } if(a==b) cout<<"YES";//判断 else cout<<"NO"; return 0; }
Information
- ID
- 291
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 139
- Accepted
- 44
- Uploaded By