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

使用C++ STL vector和map容器超时,求分析

Posted by maximdx at 2011-12-19 18:18:27 on Problem 1002
#include <iostream>
#include <vector>
#include <map>

char parse(const char &ch) {
	switch(ch){
		case '2':
		case 'A':
		case 'B':
		case 'C': return '2';
		case '3':
		case 'D':
		case 'E':
		case 'F': return '3';
		case '4':
		case 'G':
		case 'H':
		case 'I': return '4';
		case '5':
		case 'J':
		case 'K':
		case 'L': return '5';
		case '6':
		case 'M':
		case 'N':
		case 'O': return '6';
		case '7':
		case 'P':
		case 'R':
		case 'S': return '7';
		case '8':
		case 'T':
		case 'U':
		case 'V': return '8';
		case '9':
		case 'W':
		case 'X':
		case 'Y': return '9';
		case '0': return '0';
		case '1': return '1';
		default:  return '\0';
	}
}

int main( int argc, char *argv[] ) {
	using namespace std;
	vector<string> telebook;
	map<string, int> entries;
	int total_count;
	cin >> total_count;
	for( int i = 0; i < total_count; ++i ) {
		string temp;
		cin >> temp;
		telebook.push_back(temp);
	}
	vector<string>::iterator it;
	for( it = telebook.begin(); it != telebook.end(); ++it ) {
		string temp;
		char ch;
		map<string, int>::iterator map_it;
		for( int i = 0; i < it->length(); ++i ) {
			if( ( ch = parse(it->at(i)) ) != '\0' )
				temp.push_back(ch);
		}
		if( (map_it = entries.find(temp)) != entries.end() ) {
			(map_it->second)++;
		}
		else {
			entries.insert( pair<string, int>(temp, 1) );
		}
	}
	if( entries.size() == total_count )
		cout << "No duplicates." << endl;
	else {
		map<string, int>::iterator m_it;
		for( m_it = entries.begin(); m_it != entries.end(); ++m_it ) {
			if( m_it->second != 1 ) {
				string temp(m_it->first);
				temp.insert(3, "-");
				cout << temp << " " << m_it->second << 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