[模拟]Perfect Hash UVA188



 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 [模拟]Perfect Hash UVA188, where C is a positive integer you are to discover, w is an integer representation of an input word, and nis the length of the table. C must be as small as possible. Note that [模拟]Perfect Hash UVA188 is the floor function and that [模拟]Perfect Hash UVA188for some real number R is the largest integer that is [模拟]Perfect Hash UVA188 .

Here are Wally‘s notes on the subject:

Let [模拟]Perfect Hash UVA188 consist of positive integers [模拟]Perfect Hash UVA188 . The problem is to find the smallest positive integer C such that

[模拟]Perfect Hash UVA188 for all [模拟]Perfect Hash UVA188 .

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

If some

[模拟]Perfect Hash UVA188 for all [模拟]Perfect Hash UVA188 ,

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

[模拟]Perfect Hash UVA188

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, [模拟]Perfect Hash UVA188 , `z‘ to be 26. Use 5 bits for each letter (shift left by 5 or multiply by 32). Thus `a‘ = 1, `bz‘ = [模拟]Perfect Hash UVA188 .

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

直接模拟就可以了,按照题目给出的方法模拟,比较简单。

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;

int arry[20];

int convert(string str)
{
    int sum=0;
    for(int i=0;i<str.size();i++)
    {
        sum=sum*32+(str[i]-‘a‘+1);
    }
    return sum;
}

int main()
{
    string str;
    while(getline(cin,str))
    {
        memset(arry,0,sizeof(arry));
        int i,j,k=0;
        int tot;
        int minv=0x7FFFFFFF;
        string st="";
        for(i=0;i<str.size();i++)
        {
            if(str[i]!=‘ ‘)
            {
                st=st+str[i];
                if((i<str.size()-1&&str[i+1]==‘ ‘)||i==str.size()-1)
                {
                    tot=convert(st);
                    arry[k++]=tot;
                    if(tot<minv) minv=tot;
                    //cout<<tot<<endl;
                    st="";
                }
            }
        }
        int cnt=minv;
        while(1)
        {
            int tag=0;
            for(i=0;i<k;i++)
            {
                for(j=i+1;j<k;j++)
                {
                    if((cnt/arry[i])%k==(cnt/arry[j])%k)
                    {
                        cnt=min((cnt/arry[i]+1)*arry[i], (cnt/arry[j]+1)*arry[j]);
                        tag=1;
                    }
                }
            }
            if(!tag) break;
        }
        cout<<str<<endl;
        cout<<cnt<<endl<<endl;
    }
    return 0;
}


[模拟]Perfect Hash UVA188

上一篇:网页设计——7.css的入门


下一篇:MongoDB学习之--增删改查(1)