2011-11-25
典型的字符串处理问题。
刚开始用的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;
}
?