Under my umbrella.

SKII

Less is more.

洛谷 P1540 机器翻译 (C/C++, JAVA)

洛谷 P1540 机器翻译

/*
  P1540 机器翻译
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  vector<int> v;
  int m, n; cin >> m >> n;
  int cnt = 0;
  for(int i = 0; i < n; i++) {
    int a; scanf("%d", &a);
    if(find(v.begin(), v.end(), a) == v.end()) {
      if(v.size() < m) {
        v.push_back(a);
      } else {
        v.erase(v.begin()); v.push_back(a);
      }
      cnt ++; // search in outside, cause cache has not The Word.
    } else {
      continue;
    }
  }
  cout << cnt << endl;

  return 0;
}

import java.util.Scanner;
import java.util.ArrayDeque;
import java.util.Iterator;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt(), n = scan.nextInt();
        ArrayDeque<Integer> dq = new ArrayDeque<Integer>();
        int cnt = 0;
        for(int i = 0; i < n; i++) {
            int a = scan.nextInt();
            boolean having = false;
            Iterator<Integer> iter = dq.iterator();
            while(iter.hasNext()) {
                if((int)iter.next() == a) {
                    having = true; break;
                }
            }
            if(having == false) {
                if(dq.size() < m) {
                    dq.addLast(a);
                } else {
                    dq.removeFirst();
                    dq.addLast(a);
                }
                cnt++;
            } else {
                continue;
            }
        }
        System.out.println(cnt);
    }
}
posted @ 2020-01-18 15:47  NLYang  阅读(186)  评论(0编辑  收藏  举报