题解 | 机器翻译
机器翻译
https://www.nowcoder.com/practice/45ecfecd83104f37a685016361be504c
使用集合避免在缓存中进行n次查询,缩短至logn次
#include <iostream>
#include<queue>
#include<set>
using namespace std;
queue<int> fr;
set<int> fi;
int main() {
int m ,n;cin >> m >> n;
int ai;
int times =0;
while(n--){
cin >> ai;
if (fi.find(ai) == fi.end()){ // 缓存找不到
times++;
fr.push(ai);fi.insert(ai);
if(fi.size() == m+1){ // 缓存超量
fi.erase(fr.front());
fr.pop();
}
}
}cout << times;
}
查看28道真题和解析
