Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我用的map<string ,int > ,结果TLE,是不是因为字符串的匹配比较耗时?

Posted by BlueBlood at 2005-11-19 00:44:01 on Problem 1002
#include <iostream>
#include <cstdlib>
#include <string>
#include <map>

using namespace std;

map<string,int> telebook;
map<string,int>::iterator it;

string tran(string & s1)
{
	char tmp[8];
	char * s = new char[s1.length()+1];
	s[s1.length()] = 0;
	for (int a = 0; a < s1.length(); a++)
		s[a] = s1[a];

	for (int i = 0, j = 0; i < s1.length(); i++)
	{
		if (j == 3)
			tmp[j++] = '-';

		switch (s[i]) {
		case 'A': case 'B': case 'C':
			tmp[j] = '2';
			j++;
			break;
		case 'D': case 'E': case 'F':
			tmp[j] =  '3';
			j++;
			break;
		case 'G': case 'H': case 'I':
			tmp[j++] =  '4';
			break;
		case 'J': case 'K': case 'L':
			tmp[j++] =  '5';
			break;
		case 'M': case 'N': case 'O':
			tmp[j++] =  '6';
			break;
		case 'P': case 'R': case 'S':
			tmp[j++] =  '7';
			break;
		case 'T': case 'U': case 'V':
			tmp[j++] =  '8';
			break;
		case 'W': case 'X': case 'Y':
			tmp[j++] =  '9';
			break;
		default:
			if (s[i] != '-')
			tmp[j++] = s[i];
			break;
		};
	}

	string s2(&tmp[0],&tmp[8]);
	s1 = s2;
	return s;
}

int main()
{
	string s;
	int n;
	int i;
	
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> s;
		
		tran(s);
		
		telebook[s]++;
	}
	
	bool dup = false;	
	for (it = telebook.begin(); it != telebook.end(); it++)
	{
		if ((*it).second > 1)
		{
		cout << (*it).first << ' ' << (*it).second << endl;
		dup = true;
		}
		
	}

	if( dup == false )
         cout << "No duplicates." << endl ;

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator