zoukankan      html  css  js  c++  java
  • 洛谷 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);
        }
    }
    
  • 相关阅读:
    Python学习笔记Day24
    Python学习笔记Day23
    Python学习笔记Day22
    Python学习笔记Day21
    Python学习笔记Day19
    Python学习笔记Day18
    Python学习笔记Day17
    Python学习笔记Day16
    Python学习笔记Day15
    linux普通文件权限和系统目录权限的实践及结论
  • 原文地址:https://www.cnblogs.com/fromneptune/p/12209205.html
Copyright © 2011-2022 走看看