第一行输入两个整数
(
,
),分别表示缓存容量和文章单词数。
第二行输入
个整数
(
),表示文章中按顺序出现的单词编码。
输出一个整数,表示翻译过程中缓存未命中(查词典)的总次数。
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 次。
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))