本想练习一下模拟,不过用vector貌似可以轻松水过?(虽然还是模拟)
但突然发现貌似我并不会判断单词是否在内存中出现过?
最后还是靠度娘解决了。
代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (register int i = (a); i <= (b); i++) int n, m, word, ans = 0; vector<int> v; int read() { int x = 0, flag = 0; char ch = ' '; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') { flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return flag ? -x : x; } void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } int main() { while (~scanf("%d%d", &m, &n)) { rep(i, 1, n) { word = read(); if (std::find(v.begin(), v.end(), word) == v.end()) { v.push_back(word); ans++; } if (v.size() > m) v.erase(v.begin()); } write(ans); } return 0; }