noip2010 machine translation

The following is taken from the title face the Los Valley 1540

Topic background

Chen small computer translation software is installed on a machine, he often used this software to translate English articles.

Title Description

The principle of this translation software is very simple, it's just from start to finish, each English word in turn will be replaced with the corresponding Chinese meaning. For each English word, the software will first look in memory of the Chinese word meaning, if there is memory, translation software will use it; if there is no memory, the software will look for in the external memory in the dictionary, find out the Chinese word meaning and translation, and the translation of the word and meaning into memory for subsequent lookup and translation.

Suppose there are memory M M units, each unit can store a translated word and sense. Whenever the software is stored in memory before a new word, if the number of words already stored in the current memory is not more than M. 1- M - . 1, the software will be stored in memory unit a new word not used; if the memory saved the M M words, the software will clear the memory of the first to enter the word, freeing unit, hold the new words.

Assume an English article of length N N words. Given this article to be translated, the translation software needed to find how many times the external memory dictionary? Before assuming that translation start, there is no word in memory.

  Very simple simulation of it, wa twice, mental retardation are some very wrong, and now start all over again, we must avoid arrogance ah. Open queue to store memory words, js used to count the number of words in the memory.

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int m,n;
int ans;
int tmp;
bool a[10005];
int js;
queue<int>q;
int main()
{
    cin>>m>>n;
    for(int i=1;i<=n;++i)
    {
        cin>>tmp;
        if(a[tmp])continue;
        else if(js<m)a[tmp]=1,js++,ans++,q.push(tmp); 
        else {
            int b=q.front();
            q.pop();q.push(tmp);
            a[b]=0,a[tmp]=1,ans++;
        }
    }
    cout<<ans;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/yuelian/p/11184015.html