栈的初级应用:
题目链接:http://www.acm.uestc.edu.cn/#/contest/show/10
AC Code:
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
#define MAXN 1000000+10
int n,la[MAXN],lb[MAXN];
int main(){
int i,j,k = 1,m = 0,temp=1;
stack<int> s;
while(scanf("%d",&n)!=EOF){
for(i=1;i<=n;i++)
{
cin>>la[i];
s.push(la[k++]);
}
for(j=1;j<=n;j++)
cin>>lb[j];
k = 1;
m=1;
while(m<=n){
int top=s.top();
if(la[k]==lb[m]){ k++;m++;}
else if(top==lb[m]) {s.pop();m++;}
else if(k<=n) s.push(la[k++]);
else {temp=0; break;}
}
printf("%s\n",temp ? "Yes":"No");
}
return 0;
}