本文主要是介绍hdu 1022 Train Problem I (栈的应用),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:火车进站问题,判断出栈的顺序。
仅仅是栈的简单应用,熟练掌握即可。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
stack<int> sta;
bool judge=true;
int n,cnt,in[10],out[10],p,order[20];
void sempty(){while(!sta.empty()){sta.pop();}
}
void exchange(int number,int a){if(a>=p){for(int i=p;i<=a;i++){sta.push(in[i]);order[++cnt]=1;}p=a+1;sta.pop();order[++cnt]=-1;}else {if(number==sta.top()){sta.pop();order[++cnt]=-1;}else {judge=false;//cout<<a<<endl;return ;}}
}
int main()
{//freopen("cin.txt","r",stdin);int location;int i,j;while(cin>>n){char str[15]; scanf("%s",str);for(i=0;i<n;i++)in[i+1]=str[i]-48;cnt=0; p=1; judge=true; // p指向1,模拟队列scanf("%s",str);for(i=0;i<n;i++)out[i+1]=str[i]-48;for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(out[i]==in[j]){location=j;exchange(out[i],location);if(!judge){printf("No.\n");goto loop;}}}}printf("Yes.\n");for(i=1;i<=cnt;i++){if(order[i]>0)printf("in\n");else printf("out\n");}loop: printf("FINISH\n");sempty(); }return 0;
}
这篇关于hdu 1022 Train Problem I (栈的应用)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!