P1540 机器翻译题解

题目传送门

C++代码

#include <bits/stdc++.h>

using namespace std;

const int N = 1010;
int m, n, cnt;
//思路:队列+桶
bool b[N];      //判断该数是否在内存中,桶。
queue<int> q;   //定义一个队列
int main() {
    cin >> m >> n;
    for (int i = 1; i <= n; i++) {
        int t;
        cin >> t;
        if (!b[t]) {
            q.push(t);
            b[t] = true;
            cnt++;
            if (q.size() > m) {
                b[q.front()] = false;
                q.pop();
            }
        }
    }
    printf("%d", cnt);//输出计数
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15075102.html