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 flyingfish at 2004-07-23 18:27:00 on Problem 1583
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define MULTIPLIER 37
#define NHASH 500
int max=0;
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;
	int num;
	Nameval *next;
};
Nameval *symtab[NHASH];
int lookup(char *name)
{
	int h,i;
	Nameval *sym;
	h=hash(name);
	for(i=0;name[i]!='\0';i++)
		if(name[i]>='A'&&name[i]<='Z')
			name[i]+=32;
	for(sym=symtab[h];sym!=NULL;sym=sym->next)
		if(strcmp(name,sym->name)==0)
		{
			sym->num++;
			if(sym->num>max)
				max=sym->num;
			return 0;
		}
	sym=(Nameval *)malloc(sizeof(Nameval));
	sym->name=name;
	sym->num=1;
	sym->next=symtab[h];
	symtab[h]=sym;
	return 0;
}
int main()
{
	char *tmp,*a[100];
	int size,i,j;
	char arry[100],*name;
	Nameval *sy;
	while(scanf("%s",arry)!=EOF)
	{
		size=strlen(arry);
		for(i=size-1;i>0;i--)
			if((arry[i]>='a'&&arry[i]<='z')||(arry[i]>='A'&&arry[i]<='Z')||(arry[i]>='0'&&arry[i]<='9'))
				break;
		arry[i+1]='\0';
		name=(char *)malloc(strlen(arry)+1);
		strcpy(name,arry);
		lookup(name);
	}
	printf("%d occurrences\n",max);
	j=0;
	for(i=0;i<NHASH;i++)
		for(sy=symtab[i];sy!=NULL;sy=sy->next)
			if(sy->num==max)
				a[j++]=sy->name;
	max=j;
	for(i=0;i<max;i++)
		for(j=i+1;j<max;j++)
			if(strcmp(a[i],a[j])>0)
			{
				tmp=a[i];
				a[i]=a[j];
				a[j]=tmp;
			}
	for(i=0;i<max;i++)
		printf("%s\n",a[i]);
}

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