90求解了
查看原帖
90求解了
1057133
koromu楼主2023/10/11 10:44
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
  
#include <queue>
  
using namespace std;
  
//M个单位,每个单位一个单词和译义, 最新的单词存入前,如果等于M, 那么删除最早的单词

//每个数字代表一个英文单词

//M是内存容量 N是文章长度

//M表示要注意删除前面的单词 N表示要输入的单词个数

//最后输出查字典的个数

//例题使用哈希表

int main()
{
	int arr[1005] = {0};

	int count = 0;
	
	queue<int> q1;
	int M, N;
	cin >> M >> N;
	for (; N > 0; N--)
	{
		int temp; 
		cin >> temp;
		if (q1.front() != temp)
		{
			if (q1.size() == M)
			{
				
				arr[q1.front()] = 0;
				q1.pop();
				
			}
			
		if (arr[temp] == 0)
		{
			q1.push(temp);
			arr[temp] = 1;
			count++;
		}
		
		}
	}

	cout << count << endl;
	return 0;
}

差一个案例

2023/10/11 10:44
加载中...