30分,求助
查看原帖
30分,求助
920435
yhxcj楼主2023/10/18 13:13

虽然我已经AC了这道题,但用的是第二版代码,第一版只有30分,所以还是想请教一下各位大佬我的第一版代码有什么问题(我第二版代码换了一种思路才过的)

#include <cstdio>
#include <queue>
#include <set>
using namespace std;
queue<int> words;
set<int> dictionary;

int main() {
	int m, n, total = 0, word;
	scanf("%d%d", &m, &n);

	for (int i = 1; i <= n; i++) {

		scanf("%d", &word);
		words.push(word);

		if (dictionary.find(word) == dictionary.end()) {
			if (dictionary.size() >= m) {
				dictionary.erase(words.front());
				words.pop();
			}

			dictionary.insert(word);
			total++;
		}
	}

	printf("%d", total);
	return 0;
}
2023/10/18 13:13
加载中...