1. 程式人生 > >【洛谷OJ】【JAVA】 P1540 機器翻譯

【洛谷OJ】【JAVA】 P1540 機器翻譯

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	private static Scanner cin;
	
	public static void main(String args[]) throws Exception {
		cin = new Scanner(System.in);
		int m = cin.nextInt();
		Dictionary dic = new Dictionary(m);
		int n = cin.nextInt();
		int tmp;
		for(int i=0;i<n;i++) {
			tmp = cin.nextInt();
			dic.search(tmp);
		}
		System.out.println(dic.getSearchOutside());
	}
}

class Dictionary{
	LinkedList<Integer> list = new LinkedList<Integer>();
	private static int searchOutside = 0;
	private int size;
	
	public Dictionary(int size) {
		this.size = size;
	}
	
	public void search(int i) {
		if(list.contains(i)) {
			//do nothing
		}else {
			searchOutside++;
			if(list.size()>=size) {
				list.removeFirst();
				list.add(i);
			}else {
				list.add(i);
			}
		}
	}

	public int getSearchOutside() {
		return searchOutside;
	}
	
	
}