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

那我就不知了,我也想不通为什么会MLE,TLE,我发一下程序,帮我看看怎么优化

Posted by flyingfish at 2004-07-26 18:34:40 on Problem 1200
In Reply To:他们现在过的都是用什么过的? Posted by:hawk at 2004-07-26 18:32:35
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define MULTIPLIER 31
#define NHASH 30000
int a,b;
unsigned int hash(char *str)
{
	unsigned int h;
	unsigned char *p;
	h=0;
	for(p=(unsigned char *)str;*p!='\0';p++)
		h=MULTIPLIER*h+*p;
	return h%NHASH;
}
typedef struct Nameval Nameval;
struct Nameval
{
	char *name;
	Nameval *next;
};
Nameval *symtab[NHASH];
int lookup(char *name)
{
	int h;
	char *p;
	Nameval *sym;
	h=hash(name);
	for(sym=symtab[h];sym!=NULL;sym=sym->next)
		if(strcmp(name,sym->name)==0)
			return 1;
	p=(char *)malloc(a+1);
	strcpy(p,name);
	sym=(Nameval *)malloc(sizeof(Nameval));
	sym->name=p;
	sym->next=symtab[h];
	symtab[h]=sym;
	return 0;
}
int main()
{
	int i;
	long sum=0;
	char word,*p,*q;
	scanf("%d%d",&a,&b);
	getchar();
	p=(char *)malloc(a+1);
	for(i=1;i<a;i++)
		*(p+i)=getchar();
	while(scanf("%c",&word)!=EOF&&word!=10)
	{
		for(i=0;i<a-1;i++)
			*(p+i)=*(p+i+1);
		*(p+a-1)=word;
		*(p+a)='\0';
		if(lookup(p)==0)
			sum++;
	}
	printf("%ld\n",sum);
}

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