一个学习栈的题目。
#include <iostream>
#include <cstdio>
#include <stack>
#include<string.h>
#include<stdio.h>
using namespace std;
#define MAXN 1000000+10
int n,hehe[MAXN],sb[MAXN];
int main()
{
int i,j,k=1,m=0,ok=1;
stack<int>q; //声明了一个储存int型元素的栈,栈名是q
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&hehe[i]);
q.push(hehe[k++]);
}
for(j=1;j<=n;j++)
scanf("%d",&sb[j]);
k=1;
m=1;
while(m<=n)
{
int top=q.top(); //top=栈底元素
if(hehe[k]==sb[m])
{
k++;
m++;
}
else if(top==sb[m])
{
q.pop();//出栈
m++;
}
else if(k<=n)
q.push(hehe[k++]);
else
{
ok=0;
break;
}
}
printf("%s\n",ok?"Yes":"No");
}
return 0;
}