[模拟]Perfect Hash UVA188
来源:互联网 发布:麦当劳 知乎 编辑:程序博客网 时间:2024/05/17 03:11
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 nis 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 outa bee see deethe of and to a in that is i it with for as
Sample output
this is a test of some words to try out17247663a bee see dee4427the of and to a in that is i it with for as667241
直接模拟就可以了,按照题目给出的方法模拟,比较简单。
#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
- uva188 Perfect Hash
- uva188 - Perfect Hash(完美哈希)
- uva 188 - Perfect Hash 模拟
- UVA188
- Uva 188 Perfect Hash
- UVa 188 - Perfect Hash
- uva 188 - Perfect Hash
- UVa 188 - Perfect Hash
- 188 - Perfect Hash
- UVA 188 Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 Perfect Hash
- 188 - Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 - Perfect Hash
- UVA - 188 Perfect Hash
- uva 188 Perfect Hash
- FineUI 基于 ExtJS 的开源 ASP.NET 控件库
- windows cmd 递归计算某目录下文件数目
- openjudge 计算概论 函数与字符串练习(1)2:字符串排序
- 出现Note: Test.java uses unchecked or unsafe operations.Note: Recompile
- 九度oj 题目1447:最短路
- [模拟]Perfect Hash UVA188
- “add measurements”(添加度量)菜单问题
- openjudge 计算概论 字符串1:统计字符数
- java.util.Stack创建栈
- 基于 RBAC 的 Web Security Framework 的研究与应用
- VS2010中使用Modeling Project定制DSL以及自动化代码生成
- 音速启动(VStart)中使用相对路径存储应用链接
- 微信调用接口,防止Access_token过期的方法
- Delphi6-xe5 中的MD5实现方法