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

超时问题

Posted by pg20047 at 2004-09-11 19:16:12 on Problem 1002
我提交的程序总是超时,请诸位指教如何提高程序效率。谢谢。
源代码如下:
#include <stdio.h>
#include <string.h>
struct TELITEM
{
	char tel[8];
	int num;
	TELITEM* pNext;
};


void inline standard(char longtel[], char stdtel[])
{
	int tellen = strlen(longtel);
	int stdindex = 0;
	for(int subindex=0; subindex<tellen; subindex++)
	{
		char dig = longtel[subindex];
		if(dig>='0' && dig<='9')
		{
			stdtel[stdindex] = dig;
			stdindex++;
		}
		else if(dig>='A' && dig<='O')
		{
			stdtel[stdindex] = '2'+(dig-'A')/3;
			stdindex++;
		}
		else if(dig=='P')
		{
			stdtel[stdindex] = '7';
			stdindex++;
		}
		else if(dig>='R' && dig<='Y')
		{
			stdtel[stdindex] = '7'+(dig-'Q')/3;
			stdindex++;
		}
	}

	stdtel[stdindex] = 0;
}

void inline compare(TELITEM*& telnums, int& count, char tel[])
{
	TELITEM* p = telnums;
	TELITEM* q = NULL;
	while(p && strcmp(p->tel, tel)<0)
	{
		q = p;
		p = p->pNext;
	}
	if(!p || strcmp(p->tel, tel)>0)
	{
		count++;
		TELITEM* pNew = new TELITEM;
		pNew->num = 1;
		strcpy(pNew->tel, tel);
		pNew->pNext = p;
		q?q->pNext = pNew:telnums=pNew;
		
	}
	else
	{
		p->num++;	
	}	
}

void inline printtel(TELITEM* telnums, int count)
{
	bool dup = false;
	TELITEM* p = telnums;
	while(p)
	{
		if(p->num>1)
		{
			dup = true;
			for(int subidx = 0; subidx<3; subidx++)
				printf("%c", p->tel[subidx]);
			printf("-");
			for(subidx=3; subidx<7; subidx++)
				printf("%c", p->tel[subidx]);
			printf(" %d\n", p->num);
		}

		p = p->pNext;
	}

	if(!dup)
		printf("No duplicates.\n");
}

void main()
{
	TELITEM* pTels = NULL;
	int count = 0;
	int total;
	scanf("%ud", &total);
	for(int index=0; index<total; index++)
	{
		char temptel[255];
		scanf("%s", temptel);
		char tel[8];
		standard(temptel, tel);
		compare(pTels, count, tel);
	}

	printtel(pTels, count);
}

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