输入
有n节车箱,n=0为全部工作结束;
再输入A站出车顺序:
再输入B站驶入顺序;
输出
可以调度输出Yes,不可以输出No
样例输入
5
1 2 3 4 5
5 4 3 2 1
5
1 2 3 4 5
5 4 1 2 3
6
1 2 3 4 5 6
5 4 3 2 1 6
6
1 2 3 4 5 6
4 3 2 1 6 5
0
样例输出
Yes
No
Yes
Yes
code
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
int main()
{
int n;
while (cin >> n && n) {
vector<int> pre(n+1);
for (int i = 1; i <= n; i++)
cin >>pre[i];
vector<int> target(n+1);
bool flag = true;
for (int i = 1; i <= n; i++)
cin>>target[i];
stack<int> C;
int A = 1, B = 1;
while (B <= n) {
if (pre[A] == target[B]) {
B++;
A++;
}
else if (!C.empty() && C.top() == target[B]) {
B++;
C.pop();
}
else if (A<=n) {//放入栈C中
C.push(pre[A++]);
}
else {//上述情况都没遇上直接break
flag = false;
break;
}
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
target.clear();
pre.clear();
while(C.size()){
C.pop();
}
}
return 0;
}