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

指针链表 time limit exceeded 还有高手看这题吗????帮帮帮忙

Posted by corn0329 at 2005-07-12 15:23:06 on Problem 1002
#define LEN sizeof(struct key)
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
	struct key{
	char code[8];
	int m;
	struct key *next;
	};
struct key*insert(struct key*head,char co[])
{
	struct key *p,*p1,*p2;
	if(head==NULL)
	{
		head=(struct key*)malloc(LEN);
		strcpy(head->code,co);
		head->m=1;
		head->next=NULL;
		return head;
	}
	p2=p1=head;
	p=(struct key *)malloc(LEN);
	strcpy(p->code,co);
	p->m=1;
	while(strcmp(p->code,p1->code)>0&&p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;
	}
	if(strcmp(p->code,p1->code)==0)
		{(p1->m)++;free(p);}
	else if(strcmp(p->code,p1->code)<0)
	{
		if(head==p1)head=p;
		else p2->next=p;
		p->next=p1;
	}
	else{
		p1->next=p;
		p->next=NULL;
	}

	return(head);
}
void list(struct key*head)
{
	struct key*p;
	int i,flag=1;
	p=head;
	while(p!=NULL)
	{
		if(p->m>=2)
		{
			flag=0;
			for(i=0;i<=2;i++)
				printf("%c",p->code[i]);
			printf("-");
			for(i=3;i<=6;i++)
				printf("%c",p->code[i]);
			printf(" %d\n",p->m);
		}
		p=p->next;
	}
	if(flag)printf("No duplicates.");
}
main()
{

	char c,code1[8]={'\0'};
	int con[]={'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','7','7','7','8','8','8','9','9','9'};
	int n,i,j;
	struct key *head;
	scanf("%d",&n);
	getchar();
	head=NULL;
	for(i=0;i<n;i++)
	{
		j=0;
		scanf("%c",&c);
		while(c!='\n')
		{
			if((c>='A'&&c<='P')||(c>='R'&&c<='Y')||(c<='9'&&c>='0'))
				if(isalpha(c))
				{
					code1[j]=con[c-'A'];
					j++;
				}
				else {
					code1[j]=c;
					j++;
				}
			scanf("%c",&c);
		}
		code1[j]='\0';
		head=insert(head,code1);
	}
	list(head);
}


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