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

哪位大哥帮忙看下,总是超时(g++)

Posted by onewayjiang at 2012-05-12 14:44:24 on Problem 1002
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;
int phone_count[100001]={0};
int num[25] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9};

int transfer(string phoneStr);
int main()
{
	int N,i,j;
	string phoneStr;
	bool flag = true;
	cin >> N;
	for(i=0;i<N;i++)
	{
		cin >> phoneStr;
		int phoneNum = transfer(phoneStr);
		phone_count[i] = phoneNum;
	}
	sort(phone_count,phone_count+N);

	i=0;	
	for(j=0;j<N;++j)
	{
		if(phone_count[i] != phone_count[j])
		{
			if(j-i >= 2)
			{
				flag = false;
printf("%03d-%04d %d\n",phone_count[i]/10000,phone_count[i]%10000,j-i);			  
			}
			i=j;
		}
	}
	if(j - i >=2)
	{
		flag = false;
printf("%03d-%04d %d\n",phone_count[i]/10000,phone_count[i]%10000,j-i);	
			
	}
	if(flag)cout << "No duplicates." << endl;
	return 0;
}

int transfer(string phoneStr)
{
	int result = 0;
	int i,k=0;
	int len = phoneStr.size();
	for(i=0;i<len&&k!=7;i++)
	{
		if('0'<=phoneStr[i] && phoneStr[i] <= '9')
		{
			result =result*10+(phoneStr[i]-'0');
			k++;
		}
		else if('A'<=phoneStr[i] && phoneStr[i] < 'Z')
		{
			result=result*10 + num[phoneStr[i]-'A'];
			k++;
		}
	}
	return result;
}

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