第一行输入两个整数
(
,
),分别表示缓存容量和文章单词数。
第二行输入
个整数
(
),表示文章中按顺序出现的单词编码。
输出一个整数,表示翻译过程中缓存未命中(查词典)的总次数。
3 7 1 2 1 5 4 4 1
5
翻译过程示例(缓存状态记录自左向右为最早到最近):
初始:空
1: miss,缓存->[1]
2: miss,缓存->[1,2]
1: hit,缓存->[1,2]
5: miss,缓存->[1,2,5]
4: miss,缓存->[2,5,4](替换1)
4: hit,缓存->[2,5,4]
1: miss,缓存->[5,4,1](替换2)
共 miss 5 次。
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
queue<int>q;
int m,n,x;
bool find(int x)
{
bool ju=0;
if (q.empty()) return 0;
int l=q.size();
for (int i=0;i<l;++i)
{
if (q.front()==x)
{
ju=1;
}
q.push(q.front());
q.pop();
}
return ju;
}
int main() {
int ans=0,now=0;
cin>>m>>n;
while(n--)
{
cin>>x;
if(!find(x))
{
// W1W2YYMW5W4W4YM5
// W1W2YYMW5W4YMW15
q.push(x);
ans++;
now++;
if (now>m)
{
q.pop();
now--;
}
}
}
cout<<ans;
}
// 64 位输出请用 printf("%lld") M, N = map(int, input().split()) queue = list(map(int, input().split())) huancun_list = [] count = 0 for _ in range(N): if (queue[0] not in huancun_list) & (len(huancun_list) < M): huancun_list.append(queue[0]) queue.pop(0) count += 1 elif (queue[0] in huancun_list) & (len(huancun_list) < M): queue.pop(0) continue elif (queue[0] not in huancun_list) & (len(huancun_list) >= M): huancun_list.pop(0) huancun_list.append(queue[0]) queue.pop(0) count += 1 elif (queue[0] in huancun_list) & (len(huancun_list) >= M): queue.pop(0) continue print(count)
#include <iostream>
using namespace std;
#include <vector>
#include <queue>
#include <algorithm>
int main(){
int M, N;
vector<int> v;
vector<int> cache;
cin >> M >> N;
int num, count = 0;
for(int i = 0; i < N; ++i){
cin >> num;
v.push_back(num);
}
for(const auto& val : v){
vector<int>::const_iterator it = find(cache.begin(), cache.end(), val);
if(it == cache.end()){
if(cache.size() < M) cache.push_back(val);
else{
cache.erase(cache.begin());
cache.push_back(val);
}
count++;
}
}
cout << count << endl;
return 0;
} #include <iostream>
using namespace std;
#include<deque>
#include<vector>
bool isInCache(int a,deque<int>d){
for(int i=0;i<d.size();i++){
if(a==d[i]){
return 1;
}
}
return 0;
}
int main() {
int M,N;
cin>>M>>N;
vector<int>v;
for(int i=0;i<N;i++){
int a;
cin>>a;
v.push_back(a);
}
int miss=0;
deque<int>d;
for(int i=0;i<v.size();i++){
if(isInCache(v[i],d)){
continue;
}
else if(!isInCache(v[i],d)){
miss++;
if(d.size()==M){
d.pop_front();
d.push_back(v[i]);
}
else{
d.push_back(v[i]);
}
}
}
cout<<miss;
}
// 64 位输出请用 printf("%lld") class Queue:
def __init__(self):
self.in_stack: list[int] = []
self.out_stack: list[int] = []
def push(self, x: int):
self.in_stack.append(x)
def _dump_if_needed(self):
if not self.out_stack:
while self.in_stack:
self.out_stack.append(self.in_stack.pop())
def pop(self) -> int:
if self.isEmpty():
raise IndexError("pop from an empty queue")
self._dump_if_needed()
return self.out_stack.pop()
def query(self) -> int:
if self.isEmpty():
raise IndexError("pop from an empty queue")
self._dump_if_needed()
return self.out_stack[-1]
@property
def size(self) -> int:
return len(self.in_stack) + len(self.out_stack)
def isEmpty(self) -> bool:
return not self.in_stack and not self.out_stack
def __len__(self) -> int:
return self.size
def __contains__(self, x : int) -> bool:
return x in self.in_stack&nbs***bsp;x in self.out_stack
def totalQuery(M : int, words : list[int]) -> int:
vocab = Queue()
total_number_of_query = 0
for word in words:
if word not in vocab:
total_number_of_query += 1
if vocab.size >= M:
vocab.pop()
vocab.push(word)
return total_number_of_query
def main():
M, N = map(int, input().split())
words = list(map(int, input().split()))
print(totalQuery(M, words))
if __name__ == "__main__":
main() import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt(); // 缓存容量
int N = scanner.nextInt(); // 单词数量
int[] words = new int[N];
for (int i = 0; i < N; i++) {
words[i] = scanner.nextInt();
}
// 使用队列实现
Queue<Integer> cacheQueue = new LinkedList<>();
int missCount = 0; // 未命中次数
for (int word : words) {
if (cacheQueue.contains(word)) {
// 缓存命中,无需操作
continue;
} else {
// 缓存未命中,查词典次数加1
missCount++;
// 若缓存已满,移除最早进入的元素(队首)
if (cacheQueue.size() >= M) {
cacheQueue.poll();
}
// 将新单词加入队尾
cacheQueue.offer(word);
}
}
System.out.println(missCount);
scanner.close();
}
}
def page_replaced(l,k,pages):
cache = []
missed_count = 0
for page in pages:
if page not in cache:
missed_count += 1
if l == len(cache):
cache.pop(0)
cache.append(page)
return missed_count
l,k = map(int,input().strip().split(" "))
pages = list(map(int,input().strip().split(" ")))
print(page_replaced(l,k,pages))