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

哦~还是TLE

Posted by tend at 2005-08-31 16:50:54 on Problem 2503
In Reply To:Re:手头没有VC,我用这个马甲边改边交的...不保证改的没错误 Posted by:ykt at 2005-08-31 16:17:35
#include<stdio.h>
#include<string.h> 
#include <iostream>
using namespace std;
/*
* Macro 
*/
#define MAX_WORD_NUM 100000
#define MAX_WORD_LENGTH 10+1
#define PRIME_NUM 13

/*
* declaration of structure
*/
typedef struct HashTable_struct{
	char value[MAX_WORD_LENGTH];
	char key[MAX_WORD_LENGTH];
}HashTable;

/*
* declaration of functions
*/


/*
* global variables
*/
HashTable hashTable[MAX_WORD_NUM];


/*
*  definition of functions
*/
/*hash the string*/
int evalKey(char *p){
	int key=0; 
	while(*p){
		key+=(*p++)*PRIME_NUM;
		key=key%MAX_WORD_NUM;
	}
	
	return key; 
}

/*insert the key-value pair into the hash table*/
void insertTable(char *key,char *value){
	int keyNum=evalKey(key);
	while(hashTable[keyNum].key[0]!='\0'){
		keyNum++;
		keyNum=keyNum%MAX_WORD_NUM;
	}
	
	strcpy(hashTable[keyNum].key,key);
	strcpy(hashTable[keyNum].value,value);
	
}

/*given a key, find the position of this key , return -1 if not found*/
int findValue(char *key){
	int keyNum=evalKey(key);
	
	while(hashTable[keyNum].key[0]!='\0'){
		if(strcmp(hashTable[keyNum].key,key)==0)
			return keyNum;		
		keyNum++;
		keyNum=keyNum%MAX_WORD_NUM;
	}
	
	return -1;
	
}
void main()
{ 
	//freopen("test.txt","r",stdin);
	char english[MAX_WORD_LENGTH],dialect[MAX_WORD_LENGTH],line[MAX_WORD_LENGTH*2+1],c;
	int index;
	while(1){
		scanf("%s",&english);
		c=getchar();
		if(c!=' ')
			break;
		scanf("%s",&dialect);		
		insertTable(dialect,english);
	}
	while(1){
		index=findValue(english);
		if(index>=0)
			printf("%s\n",hashTable[index].value);
		else
			printf("eh\n");
		if(scanf("%s",&english)==EOF)
			break;
	}
}

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