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

超时,是为什么??用stl的map 感觉写起来好简单,可是就是超时!

Posted by Saleed at 2016-10-09 22:40:14 on Problem 1002
#include<iostream>
#include<map>
#include<string> 
#include<stdio.h>

using namespace std;

int main(){
	int num;
	cin>>num;
	map<string,int> out;
	map<char,char> c2d; 
	c2d['A']='2';
	c2d['B']='2';//map使用下标自动添加元素
	c2d['C']='2';
	
	c2d['D']='3';
	c2d['E']='3';
	c2d['F']='3';
	
	c2d['G']='4';
	c2d['H']='4';
	c2d['I']='4';
	
	c2d['J']='5';
	c2d['K']='5';
	c2d['L']='5';
	
	c2d['M']='6';
	c2d['N']='6';
	c2d['O']='6';
	
	c2d['P']='7';
	c2d['R']='7';
	c2d['S']='7';
	
	c2d['T']='8';
	c2d['U']='8';
	c2d['V']='8';
	
	c2d['W']='9';
	c2d['X']='9';
	c2d['Y']='9';
	
	string in;
	while(num>0){
		cin>>in;
		string id;
		for(int i=0;i<in.size();i++){
			if(in[i]>='A'&&in[i]<='Z'&&in[i]!='Q'&&in[i]!='Z'){
				id.push_back(c2d[in[i]]);
			}
			else if(in[i]>='0'&&in[i]<='9'){
				id.push_back(in[i]);///string可以使用push_back();也可以直接用'+' string类中重载了加法符号 
			}
			else;
		}
		out[id]++;
		num--;
	}
	///// 注意这里map类型的遍历方式
	for(map<string,int>::const_iterator iter=out.begin();iter!=out.end();iter++){///注意这里要用iter!=out.end();准确来讲地址最好不要比较大小! 
		if(iter->second>=2) {
			for(int i=0;i<3;i++){
				printf("%c",iter->first[i]);
				
			}
			printf("-");
			for(int i=3;i<iter->first.size();i++){
				//cout<<iter->first[i];
				printf("%c",iter->first[i]);
			}
			printf(" %d\n",iter->second);
		}
	}
} 

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