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 tangbohu222 at 2010-08-02 17:36:32 on Problem 1002
哪位高手帮忙告诉下最简单的改进方法 还有就是时间主要浪费在哪里
#include"stdio.h"
#include"string.h"
struct Tel
{
	char num[8];
	Tel* next;
	Tel* pre;
	int times;
}*head;
void init()
{
	Tel* tel1=new Tel;
	Tel* tel2=new Tel;
	tel1->times=0;
	for(int i=0;i<6;i++)
	{
		tel1->num[i]='0';
		tel2->num[i]='0';
	}
	tel1->num[6]='0';
	tel1->num[7]='\0';
	tel2->num[6]='1';
	tel2->num[7]='\0';
	tel2->times=0;
	
	tel1->next=tel2;
	tel1->pre=NULL;
	tel2->pre=tel1;
	tel2->next=NULL;
	head=tel1;
}


char translate(char in)
	{
		if(in=='1') return '1';
		else if(in=='2'||in=='A'||in=='B'||in=='C') return '2';
		else if(in=='3'||in=='D'||in=='E'||in=='F') return '3';
		else if(in=='4'||in=='G'||in=='H'||in=='I') return '4';
		else if(in=='5'||in=='J'||in=='K'||in=='L') return '5';
		else if(in=='6'||in=='M'||in=='N'||in=='O') return '6';
		else if(in=='7'||in=='P'||in=='R'||in=='S') return '7';
		else if(in=='8'||in=='T'||in=='U'||in=='V') return '8';
		else if(in=='9'||in=='W'||in=='X'||in=='Y') return '9';
		else return '0';
	}
void insert(Tel* x,Tel* z)
{    
	if(strcmp(z->num,x->num)==0) 
	{
		x->times++;
	}
	else if(strcmp(z->num,x->num)<0)
	{
		if(x->pre==NULL){x->pre=z;z->next=x;}
		else
		{
			Tel *p=new Tel;
						 p=x->pre;
					 p->next=z;
					 z->pre=p;
					 x->pre=z;
					 z->next=x;
		}
	}
	else
	{
		if(x->next==NULL) 
		{
			x->next=z;z->pre=x;
			z->next=NULL;


		}
		else insert(x->next,z);
	}
				

  }

int main()
{
	


	init();
	int is=0;
   int amount;
	Tel* p=head;
	char space=' ';      
	void insert(Tel* x,Tel* z);
	//cin>>amount;
	scanf("%d",&amount);
	getchar();
	for(int i=0;i<amount;i++)
	{
		Tel* tel=new Tel;
		int k=0;
		//char temp;
		while(true)
		{
			char temp;
			scanf("%c",&temp);
			if(temp=='-') continue;
			else if(temp=='\n') break;
			else  tel->num[k++]=translate(temp); 
		}
		tel->num[k++]='\0';
		tel->times=1;
         p=head;
		 insert(p,tel);
		 p=head;
	}
	Tel* q=head;
	while(q!=NULL)
	{
		if(q->times>1)
		{//cout<<q->num[0]<<q->num[1]<<q->num[2]<<'-'<<q->num[3]<<q->num[4]<<q->num[5]<<q->num[6]<<" "<<q->times<<endl;
         //printf("%d",q->times);
			printf("%c%c%c-%c%c%c%c %d\n",q->num[0],q->num[1],q->num[2],q->num[3],q->num[4],q->num[5],q->num[6],q->times);
		  is++;
		}
		q=q->next;
	   
	
	    }
	if(is==0) printf("No duplicates.\n");
	
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