用一个栈辅助,模拟过程+判断就可以了。
#include <iostream> #include <memory.h> #include <stack> #define LEN 100005 using namespace std; int A[LEN]; int B[LEN]; int main() { int n; while (cin >> n) { for (int i = 0; i < n; i++) { cin >> A[i]; } for (int i = 0; i < n; i++) { cin >> B[i]; } stack<int> st; int i = 0; int j = 0; while (true) { if (j == n) { cout << "Yes" << endl; break; } if (st.empty() || st.top() != B[j]) { if (i == n) { cout << "No" << endl; break; } st.push(A[i]); i++; } else { st.pop(); j++; } } } return 0; }