`
ysshuai19
  • 浏览: 14889 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

POJ 1002 解题报告 487-3279

    博客分类:
  • POJ
阅读更多

典型的字符串处理问题。

刚开始用的vector,果断超时。题目本身没什么难度,只在选择容器上要考虑。

AC咯。

 

#include <iostream>
#include <string>
#include <iomanip>
#include <map>

using namespace std;

int nicknameToNumber(string&);

int main ()
{
	int iPhoneTotal;
	cin >> iPhoneTotal;

	map<int, int> mPhone;
	while (iPhoneTotal--)
	{
		string strTemp;
		cin >> strTemp;
		++mPhone[nicknameToNumber(strTemp)];
	}

	int iFlag = 0;
	map<int, int>::iterator iter = mPhone.begin();
	while (iter != mPhone.end())
	{
		if (iter->second > 1)
		{
			iFlag = 1;
			cout << setw(3) << setfill('0') << iter->first/10000 << "-" << setw(4) << iter->first%10000 << " " << iter->second << endl;
		}
		iter++;
	}

	if (!iFlag)
	{
		cout << "No duplicates." << endl;
	}

	return 0;
}

int nicknameToNumber(string& strPhone)
{
	int r = 0;
	int i = 0;
	while (i < strPhone.size())
	{
		if ('-' == strPhone[i] || 'Q' == strPhone[i] || 'Z' == strPhone[i])
		{

		}
		else if (strPhone[i] >= 'A' && strPhone[i] <= 'P')
		{
			r = r * 10 + 2 + (strPhone[i] - 'A')/3;
		}
		else if (strPhone[i] >= 'R' && strPhone[i] <= 'Y')
		{
			r = r * 10 + 2 + (strPhone[i] - 'A' -1)/3;
		}
		else if (strPhone[i] <= '9' && strPhone[i] >= '0')
		{
			r = r * 10 + (strPhone[i] - '0');
		}

		i++;
	}
	return r;
}






0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics