您的位置:首页 > Web前端

UVA - 188 Perfect Hash(hash)

2014-08-29 11:04 330 查看



Perfect Hash

Perfect Software, Inc. has obtained a government contract to examine text flowing through a high-speed network for the occurrence of certain words. Your boss, Wally Perfect, has designed a parallel processing system which checks each word against a group of
small perfect hash tables.

A perfect hash function maps its input directly to a fully occupied table. Your job is to construct the perfect hash functions from the lists of words in each table. The hash function is of the form

,
where C is a positive integer you are to discover, w is an integer representation of an input word, and n is the length of the table. C must be as small as possible. Note that

is
the floor function and that

for some real number R is the largest integer that is

.

Here are Wally's notes on the subject:

Let

consist of positive integers

.
The problem is to find the smallest positive integer C such that


for all

.

C must be a multiple of at least one element of W.

If some


for all

,

then the next largest C that could resolve the conflict is at least



Since all such conflicts must be resolved, it is advantageous to choose the largest candidate from among the conflicts as the next C to test.

You are to convert each word to a number by processing each letter from left to right. Consider `a' to be 1, `b' to be 2,

,
`z' to be 26. Use 5 bits for each letter (shift left by 5 or multiply by 32). Thus `a' = 1, `bz' =

.

Input

Input to your program will be a series of word lists, one per line, terminated by the end-of-file. Each line consists of between two and thirteen words of at most five lower case letters each, separated from each other by at least one blank. There will always
be at least one one-letter word.

For each list, you are to print the input line. On the next line, print the C for the hash function determined by the list. Print a blank line after each C.

C will always fit in a 32-bit integer.

Sample input

this is a test of some words to try out
a bee see dee
the of and to a in that is i it with for as


Sample output

this is a test of some words to try out
17247663

a bee see dee
4427

the of and to a in that is i it with for as
667241


题目大意:

你需要构造一个完美哈希函数,该函数为 [C/w] mod n

C是一个正数, 也就是你要找的那个数(结果要输出这个数)。

w是由一个单词转换得到的数字,例如 `a' = 1, `bz' = 2*32 + 26 = 90,可以把它看成是32进制的转换。

n代表的是一行中的单词的个数。

解析:怎样求出C呢?

w = {w1,w2...wn}是由一行中的各个单词转换而来的,也就是C必须是W中某一个的倍数,且C要尽可能的小。

如果

for all

.,则C取




调用递归重新判断,直到没有重复的为止。

注意:两个单词之间可能不止一个空格,因为这个RE了好几次。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 300;
int n;
int w
;
void solve(int& c) {
	for(int i = 0; i < n; i++) {
		for(int j = i+1; j < n; j++) {
			if( c/w[i]%n == c/w[j]%n) {
				c = min(floor(c/w[i]+1)*w[i],floor(c/w[j]+1)*w[j]);
				solve(c);
				return ;
			}
		}
	}
}
int main() {
	char str
;
	while(gets(str)) {
		memset(w,0,sizeof(w));
		int len = strlen(str);
		n = 0;
		for(int i = 0; i < len; i++) {
			if(str[i] == ' ') {
				n++;
				while(str[i] == ' ') {
					i++;
				}
				i--;
			}else {
				w
 = w
 * 32 + (str[i] - 'a' + 1);
			}
		}
		n++;
		sort(w,w+n);
		int c = 0;
		solve(c);
		puts(str);
		printf("%d\n\n",c);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: