编程之美2.3题目,寻找最大的发帖水王

本文介绍了一种高效算法用于找出论坛中发帖最多的用户(水王)。通过遍历ID并利用特定的数据结构来跟踪频繁出现的ID,该算法能够在大规模数据集上实现线性时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目代码如下:

/************************************************************************/
/* 编程之美,2.3,寻找发帖水王
*/
/************************************************************************/
template<typename T>
T find(T *type, int n)
{
	T ret;
	int times = 0;
	for(int i = 0; i < n; ++i)
	{
		if(times ==0)
		{
			ret = type[i];
			times = 1;
		}
		else
		{
			if(ret == type[i]) times++;
			else times--;
		}
	}

	return ret;
}

main函数的测试代码:

// 0108Array.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

#include "Array.h"
#include "maxdiff.h"



typedef std::unordered_map<std::string, int> WordCount;

struct Greater
{
	bool operator()(const std::pair<int, WordCount::iterator>& lhs,
		const std::pair<int, WordCount::iterator>& rhs) const
	{
		return lhs.first > rhs.first;
	}
};

void test()
{
	WordCount counts;
	std::string word;
	while (std::cin >> word)
	{
		counts[word]++;
	}

	typedef std::vector<std::pair<int, WordCount::iterator> > FreqList;
	FreqList freq;
	freq.reserve(counts.size());
	for (WordCount::iterator it = counts.begin(); it != counts.end(); ++it)
	{
		freq.push_back(make_pair(it->second, it));
	}

	std::sort(freq.begin(), freq.end(), Greater());
// 	for (auto item : freq)
// 	{
// 		std::cout << item.first << '\t' << item.second->first << '\n';
// 	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	//
//	test();


	vector<int> array1;

	array1.push_back(1);
	array1.push_back(2);
	array1.push_back(3);
	array1.push_back(4);
	array1.push_back(5);

	vector<int>array2;
	array2.resize(array1.size());

	multiply(array1, array2);


	int array[] = {2, 4, 1, 16/*, 7, 5, 11, 9, 4*/};
	std::cout<<maxDiff_Solution1(array, sizeof(array) / sizeof(int))<<endl;

	cout<<maxDiff_Solution2(array, sizeof(array) / sizeof(int))<<endl;

	cout<<maxDiff_Solution3(array, sizeof(array) / sizeof(int))<<endl;

	KBig(array, 0, sizeof(array) / sizeof(int) - 1, 2);

	cout<<"**************************************************************"<<endl;
	cout<<"numberOfOne: "<<numberOfOne(9)<<endl;

	cout<<"Find 最大水王2.3,编程之美"<<endl;
	int cardId[] = {2, 6, 18, 3, 2, 2, 5, 7, 2, 8, 2, 2};
	cout<<find(cardId, sizeof(cardId) / sizeof(int))<<endl;

	cout<<"Find 最大水王2.3的习题,编程之美"<<endl;
	int cardId1[] = {2, 2, 2, 2, 3, 3, 7, 4, 8, 4, 6, 6, 6, 6, 6, 3};
	cout<<find2(cardId1, sizeof(cardId1) / sizeof(int))<<endl;

//	node<int> *pNode = new node<int>[3];
	system("pause");
	return 0;
}

这个题目解决后,还有一个扩展问题。


这个题目的解决的方法和之前的问题类似,由于最多的3个ID的发帖数目超过了1/4,也就是说,每次删除3个ID之外的一个ID的时候,3个ID的数目都减一次的话,最后3个ID都还存在。因为其他的所有帖子的ID之和不到1/4。因此,这个题目的解法和上一个题目的解法还是类似的。这个解法的时间复杂度比较大了,因为每遍历一个元素的时候要到存放最大3个ID的数组中遍历一遍,那么时间复杂度为O(3*N),另一个方法就是先把所有的ID按照ID的大小排序,然后遍历一遍,时间复杂度为O(N*logN + N),所以,当N的数目比较大时,用前一种方法能把时间控制在线性时间内,比较小的时候还是第二种方法快些。

  解题思路,每次循环到一个元素的时候,判断不在这3个范围内,就把3个ID的数目都减1,如果这3个的数目为0的话,就把心的值添加进来。

template<class T>
struct node
{
	int times; //出现的次数
	T id;
};

template<typename T>
int find2(T *type, int n)
{
	if(n < 3) return - 1;

	int numbers = 3;
	node<T> *pNode = new node<int>[numbers];
	
	node<T> *p = pNode;
	for(int i = 0; i < numbers; ++i)
	{
		pNode[i].times = 0;
		/*pNode[i].id = type[0];*/
	}

	for(int i = 0; i < n; ++i)
	{
		isInList(pNode, type[i], numbers);
	}

	for(int i = 0; i < numbers; ++i)
	{
		cout<<pNode[i].id<<endl;
	}

	return -1;
}

template<typename T>
bool isInList(node<T> pNode[], T value, int num)
{
	if(pNode == NULL || num < 1) return false;

	for(int i = 0; i < num; ++i)
	{
		if(pNode[i].id == value && pNode[i].times != 0)
		{
			pNode[i].times++;
			return true;
		}
	}

	for(int i = 0; i < num; ++i)
	{
		if(pNode[i].times == 0)
		{
			pNode[i].times = 1;
			pNode[i].id = value;
			return true;
		}
	}

	for(int i = 0; i < num; ++i) pNode[i].times--;

	return false;
}

在上面的main函数中有测试的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值