#include <iostream>
#include <vector>
#include <stack> //栈
#include <queue> //队列
using namespace std;
/*
知识点一:栈的基础用法
栈的功能和特点
先入后出,后入先出
//创建栈
stack <int> stk;
//添加数据
stk.push(1);
stk.push(0);
stk.push(2);
stk.push(3);
stk.push(4);
stk.push(5);
//删除元素
// stk.pop();
//判断是否为空
cout<<stk.empty()<<endl;
//查看元素数量
cout<<stk.size()<<endl;
//遍历栈
/*
int n = stk.size();
for (int i=0; i<n; i++){
cout<<stk.top()<<" ";
stk.pop();
}
while (!stk.empty()){
cout<<stk.top()<<" ";
stk.pop();
}
输入一行英文(只包含字母和空格,单词间以单个空格分隔),
将所有单词的顺序倒排并输出,依然以单个空格分隔。
//输入
string words;
getline(cin, words);
//运算
stack <string > word;
string w="";
for (int i=0; i<words.size(); i++){
if ( words[i]!=' ' ) w+=words[i];
else{
word.push(w);
w="";
}
}
word.push(w);
//输出
while ( !word.empty() ){
cout<<word.top()<<" ";
word.pop();
}
练习
将一个数组中的前k项按逆序重新存放。
例如,将数组8,6,5,4,1前3项逆序重放得到5,6,8,4,1。
int n,k;
cin>>n>>k;
vector <int> nums(n);
for (int i=0; i<n; i++) cin>>nums[i];
/*
stack <int> stk;
for (int i=0; i<k; i++) stk.push(nums[i]);
while ( !stk.empty() ){
cout<<stk.top()<<" ";
stk.pop();
}
for (int i=k; i<n; i++) cout<<nums[i]<<" ";
for (int i=k-1; i>=0; i--) cout<<nums[i] <<" ";
for (int i=k; i<n; i++) cout<<nums[i]<<" ";
知识点二:队列
陷入先出,后入后出
//创建
queue <int> que ;
//添加
que.push(1);
que.push(3);
que.push(6);
que.push(8);
que.push(2);
//查看队头和队尾数据
cout<<que.front()<<" "<<que.back();
que.pop();
while(!que.empty()){
cout<<que.front()<<" ";
que.pop();
}
*/
int main() {
int n;
cin>>n;
stack <int> stk;
for (int i=0; i<n; i++){
int s ;
cin>>s;
stk.push(s);
}
queue <int> que;
for (int i=0; i<n; i++){
int q;
cin>>q;
que.push(q);
}
while (!stk.empty()){
int s = stk.size();
for (int i=0; i<s; i++){
if(stk.top() == que.front()){
stk.pop();
que.pop();
}
else{
int temp = que.front();
que.push(temp);
que.pop();
}
}
if (s == stk.size()){
break;
}
}
cout<<stk.size();
return 0;
}