整型数组处理算法(十二)请实现一个函数:最长顺子。[风林火山]

请实现一个函数:最长顺子;输入很多个整数(1<=数值<=13),
返回其中可能组成的最长的一个顺子(顺子中数的个数代表顺的长度); 其中数字1也可以代表14;
顺子包括单顺\双顺\3顺;
单顺的定义是连续5个及以上连续的数,比如1,2,3,4,5、3,4,5,6,7,8和10,11,12,13,1等;
双顺的定义是连续3个及以上连续的对(对:两个相同的数被称为对),
比如1,1,2,2,3,3、4,4,5,5,6,6,7,7和11,11,12,12,13,13,1,1等;
3顺的定义是连续2个及以上连续的3张(3张:3个相同的数被称为3张),
比如1,1,1,2,2,2、3,3,3,4,4,4,5,5,5,6,6,6和13,13,13,1,1,1等等; 

比如:输入数组[1,5,2,3,4,4,5,9,6,7,2,3,3,4], 输出数组[2,2,3,3,4,4,5,5]


实现代码如下:

 

int putList(int k, map<int, map<int, int>* >& listData, map<int, int>* mapData) 
{  
	int nFlag =0;
	if (0 == k && mapData->size() >= 5) 
	{  
		nFlag =1;
		//listData.put(mapData.size(), mapData);  
		listData.insert(pair <int, map<int, int>* >( mapData->size(), mapData));
	}  
	if (1 == k && mapData->size() >= 3) 
	{  
		nFlag =1;
		//listData.put(2 * mapData.size(), mapData);  
		listData.insert(pair <int,  map<int, int>* >(2* mapData->size(), mapData));
	}  
	if (2 == k && mapData->size() >= 2) 
	{  
		nFlag =1 ;
		//listData.put(3 * mapData.size(), mapData); 
		listData.insert(pair <int, map<int, int>* >( 3*mapData->size(), mapData));
	}  

	return nFlag;
}

map<int, int>* getList(int* count, int k, int num, int& nMaxCount) 
{  
	map<int, map<int, int>* > listData;//= new map<int, map<int, int>*>();  
	map<int, int>* mapTemp = NULL;  

	int flag = 0;  

	int nRet = 0;
	for (int i = 1; i < num; i++) 
	{  
		if (count[i] > k && flag == 0) 
		{  
			flag = 1;  
			mapTemp = new map<int, int>;
			//mapTemp.put(i, count[i]);  
			mapTemp->insert(pair <int, int>( i, count[i]));
		} 
		else if (count[i] > k && flag == 1) 
		{  
			//mapTemp.put(i, count[i]);  
			mapTemp->insert(pair <int, int>( i, count[i]));
			if (13 == i) 
			{  
				if (count[14 - i] > k) 
				{  
					//mapTemp.put(14 - i, count[14 - i]); 
					mapTemp->insert(pair <int, int>( 14 - i, count[14 - i]));
					nRet = putList(k, listData, mapTemp); 
					
					//不是顺子,释放内存
					if (nRet==0)
					{
						delete mapTemp;
						mapTemp = NULL;
					}

				} 
				else 
				{  
					flag = 0;  
					nRet=putList(k, listData, mapTemp);  
					//不是顺子,释放内存
					if (nRet==0)
					{
						delete mapTemp;
						mapTemp = NULL;
					}
				}  
			}  
		} 
		else if (count[i] <= k && flag == 1) 
		{  
			flag = 0;  
			nRet=putList(k, listData, mapTemp);  
			//不是顺子,释放内存
			if (nRet==0)
			{
				delete mapTemp;
				mapTemp = NULL;
			}
		}  
	} 
	
	if (listData.size() > 0)  
	{
		listData.rend();
		map<int, map<int, int>* >::iterator it = listData.begin();

		nMaxCount = (*it).first;

		map<int, int>* mapReturn = (*it).second;
		map<int, int>* maptemp;

		it++;

		for (; it!=listData.end(); it++)
		{
			maptemp = (*it).second;

			delete maptemp;
			maptemp = NULL;
		}
		return mapReturn;

		//return listData[listData.size()-1];
		//return list.get(list.lastKey()); 
	}
	else  
		return NULL;  
}  

int* GetLongeststr(int* array, int nCount, int& outCount, int MaxNum) 
{
	int* count = new int[MaxNum+1];   
	memset(count, 0, MaxNum*sizeof(int));

	int nMaxLoop=0;
	int nMaxTemp;
	int nMax1Loop=0;
	int nMax2Loop=0;
	int nMax3Loop=0;
	int nMaxkey =0;

	for (int i = 0; i < nCount; i++) 
	{  
		if (array[i] < 1 || array[i] > MaxNum)   
			return NULL;  
		++count[array[i]];
	}

	map<int, map<int, int>*> allList;// = new TreeMap<Integer, Map<Integer, Integer>>();
	map<int, int>* mapTemp = NULL;  
	map<int, int>* map1Temp = NULL;
	map<int, int>* map2Temp = NULL;
	map<int, int>* map3Temp = NULL;
	for (int k = 0; k < 3; k++) 
	{  
		mapTemp = getList(count, k, MaxNum, nMaxTemp);
		if (NULL != mapTemp) 
		{  
			if (0 == k) 
			{
				//allList.put(map.size(), map); 
				//allList.insert(pair <int, map<int, int>*>( mapTemp->size(), mapTemp));
				map1Temp = mapTemp;
				nMax1Loop = nMaxTemp;
				nMaxLoop=nMaxTemp;
			}
			else if (1 == k)  
			{
				//allList.put(2 * map.size(), map);  
				//allList.insert(pair <int, map<int, int>*>( 2*mapTemp->size(), mapTemp));
				
				if (nMaxTemp>=nMaxLoop)
				{
					map2Temp = mapTemp;
					nMax2Loop = nMaxTemp;
					nMaxLoop = nMaxTemp;
				}
				else
				{
					delete mapTemp;
					mapTemp =NULL;
				}
			}
			else  
			{
				//allList.put(3 * map.size(), map); 
				//allList.insert(pair <int, map<int, int>*>( 3*mapTemp->size(), mapTemp));

				if (nMaxTemp>=nMaxLoop)
				{
					map3Temp = mapTemp;
					nMax3Loop = nMaxTemp;
					nMaxLoop = nMaxTemp;
				}
				else
				{
					delete mapTemp;
					mapTemp =NULL;
				}
			}
		}  
	}

	delete[] count;
	count = NULL;

	if (nMaxLoop>0)
	{
		if (nMaxLoop == nMax3Loop)
		{
			nMaxkey = 3;
			mapTemp = map3Temp;
		}
		else if (nMaxLoop == nMax2Loop)
		{
			nMaxkey = 2;
			mapTemp = map2Temp;
		}
		else
		{
			nMaxkey = 1;
			mapTemp = map1Temp;
		}

		outCount = nMaxLoop;
		int* result = new int[outCount]; 
		int k; 
		int nAllCount = 0;
		
		map<int, int>::iterator itorResult;
		for (itorResult = mapTemp->begin(); itorResult!=mapTemp->end(); itorResult++)
		{
			k = itorResult->first;
			for (int j =0; j<nMaxkey; j++)
			{
				
				result[nAllCount++] = k;
				
				cout << itorResult->first <<",";
			}
		}
		cout << endl;

		if (map1Temp!=NULL)
		{
			delete map1Temp;
			map1Temp = NULL;
		}

		if (map2Temp!=NULL)
		{
			delete map2Temp;
			map2Temp = NULL;
		}

		if (map3Temp!=NULL)
		{
			delete map3Temp;
			map3Temp = NULL;
		}

		return result;

	}
	else 
	{
		outCount = 0;
		return NULL;
	}
} 


有兴趣的朋友可以自己试试,仅提供参考。

 


转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/12586489



 

posted on 2013-10-11 16:16  我的小人生  阅读(599)  评论(0编辑  收藏  举报