Topic background
A machine translation software was installed on the morning computer, and he often used the software to translate English articles.
Title Description
The principle of this translation software is very simple, it is just from beginning to end, in turn each English word with the corresponding Chinese meaning to replace. For each English word, the software will first find the Chinese meaning of the word in memory, if there is memory, the software will use it for translation, if not in memory, the software will be found in the dictionary in the external memory, find out the Chinese meaning of the word and translate, and put this word and translation into memory, in case of subsequent search and translation.
Suppose there are m cells in memory, and each unit can hold a word and translate it. Whenever the software stores a new word in memory, if the number of words in the current memory is not more than M-1, the software will save the new word to an unused memory unit; If the memory has been saved with m words, the software will empty the first word into memory, freeing up the unit to store the new word.
Suppose an English article has a length of n words. Given this article to be translated, how many dictionaries does the translation software need to external memory to find? Assume that there are no words in memory before the translation begins.
Input/output format
Input Format:
Input file total 2 lines. Two numbers in each row are separated by a space.
The first behavior is two positive integers m and n, which represent the memory capacity and the length of the article.
The second behavior is n non-negative integers, in the order of the article, each number (size not more than 1000) represents an English word. The two words in the article are the same word, when and only if they correspond to the same nonnegative integer.
output Format:
Contains an integer number of times the software needs to look up the dictionary.
Input/Output sample
Input Sample # #:
3 71 2 1 5 4 4 1
Sample # # of output:
5
Description
1s per test point
There is m=1,n≤5 for 10% of the data.
There is 0<=m<=100,0<=n<=1000 for 100% of the data.
The whole dictionary process is as follows: Each line represents the translation of a word, the memory status of the translation before the colon:
Empty: The initial state of the memory is empty.
1. 1: Find word 1 and dial into memory.
2. 1 2: Find the word 2 and transfer it into memory.
3. 1 2: Find the word 1 in memory.
4. 1 2 5: Find the word 5 and transfer it into memory.
5. 2 5 4: Find word 4 and dial in memory instead of Word 1.
6. 2 5 4: Find the word 4 in memory.
7. 5 4 1: Find word 1 and dial in memory instead of Word 2.
A total of 5 dictionaries were checked.
Open a queue to simulate.
Each query is scanned once. After writing, it is more convenient to actually open a CNT array to record the number of occurrences of each element in the queue.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <algorithm>6 using namespacestd;7 intHead=0, tail=0;8 intt[ -]={0};9 intX,c=0;Ten intM,n; One intMain () { ACin>>m>>N; - inti,j; - intFlag; the for(i=1; i<=n;i++){ -scanf"%d",&x); -x + +;//read-in number plus one to prevent read-in 0 o'clock miscalculation -flag=0; + for(j=head;j<=tail;j++) - if(t[j]==x) { +flag=1; A Break; at } - if(flag)Continue; -t[++tail]=x; - if(tail-head>=m) head++; -C++; - } inprintf"%d", c); - return 0; to}
[NOIP2010] Improve group Rokua P1540 machine translation