您的位置:首页 > 其它

uva188 暴力

2015-12-23 15:50 302 查看
A - Perfect Hash
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
SubmitStatusPracticeUVA
188

Appoint description:

Description



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是由一个单词转换得到的数字,例如 `a'
= 1, `bz' =


可以把它看成是32进制的转换。 n其实就是代表的是一行中的单词的个数。

然后怎样求出C呢?

首先对于



.是由一行中的各个单词转换而来的,然后题目说C must
be a multiple of at least one element of W. 也就是C必须是W中某一个的倍数, 然后再上面点还说C must be as small as possible. C必须尽可能地小。 所以,在开始时, 让C等于w1(w1是最小的,因为W集合已经排好序了:

)。

对于C,要让它符合条件

for
all

. 所以要用一个两层for循环来判断。

如果不符合的话,就让C等于:



一直到找到符合的条件C为止,答案就出来了。

左移5位代码相当于转换成32位

w[i] = (w[i] << 5) + word[i][j] - 'a' + 1;

如果C改变了,就应该重新检验

所以用递归

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