LuoGu - p1540

machine translation

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.

Input and output formats

Input formats:

 

Total 2 2 lines. Each line separated by a space between the two numbers.

The first two acts of positive integers M, N M , the length N, and represents the memory capacity of the article.

The second behavior N N non-negative integers, in the order of articles, each of the number (size not exceeding 1000 . 1 0 0 0) represents an English word. Two words are articles in the same word, if and only if they are the same as corresponding non-negative integer.

 

Output formats:

 

An integer number of the dictionary software needs.

 

Sample input and output

Input Sample # 1: 
3 7
1 2 1 5 4 4 1
Output Sample # 1: 
5

 

Code

#include<stdio.h>
int searCnt(int mrt[],int ct[]);
int M,N;
int main(){
    scanf("%d %d",&M,&N);
    int mry[M];
    int ct[N];
    for(int i=0;i<N;i++){
        scanf("%d",&ct[i]);
    }
    int nums = searCnt(mry,ct);
    printf("%d\n",nums);
    return 0;
}

int searCnt(int mry[],int ct[]){
    int old = 0;
    int curS = 0, count = 0;
    mry[0] = ct[0]; old = 0;
    count++; curS++;
    for(int i=1;i<N;i++){
        for(int j=0;j<curS;j++){
            if(ct[i]==mry[j]){
                break;
            }else { //ct[i] != mry[j]
                if(j==curS-1){
                    if(curS<M){
                        mry[curS++] = ct[i];
                        count++; break;
                    }
                    mry[old] = ct[i];
                    old = (old+1)%M;
                    count++; break;
                }
                continue; 
            }
        }
    }
    return count;
}

 

Code thoughts

Solution one:

  I come from a friend, he thought of the queue. He will look to make a memory queue, start pointing to the head, end point to the tail. Time complexity is O (N * curS), wherein the number of the current digital CURS deposited, curS from 0 to M (M is memory space). Space complexity: the capacity to create an array of 1000.

Solution two:

  My own limited ideas (the code above), but after I turn, with time complexity is O (N * curS), space complexity is O (M + N), N for a given sequence length.

  Thought this question is actually very simple, is to determine whether a given sequence of each digit in the "Memory" already exists, if already present, you do not need to visit the "dictionary" (virtualization, does not exist), otherwise, you will want to access the dictionary. When we need to do it is in the "Memory" no match is found in the numbers, the number of visits "dictionary" of the count ++ can!

Guess you like

Origin www.cnblogs.com/kyrie211/p/11111927.html