用队列来模拟翻译软件的内存,再开一个布尔数组储存当前内存中的单词状态。
#include <iostream>
#include <queue>
using namespace std;
const int N = 1010;
int n, m;
bool st[N]; //用一个状态数组储存当前内存中有哪些单词
int main()
{
cin >> m >> n;
queue<int> q; //用队列模拟翻译软件的内存
int res = 0;
for (int i = 0; i < n; i ++ )
{
int x;
cin >> x;
if (!st[x]) //判断内存中是否已存在该单词,存在则进入下一循环
{
if (q.size() == m) //内存爆了,弹出队头
{
int t = q.front();
q.pop();
st[t] = false; //状态恢复成false
}
st[x] = true;
q.push(x);
res ++ ;
}
}
cout << res << endl;
return 0;
}