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
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
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
![](http://7xjob4.com1.z0.glb.clouddn.com/26ce5e6527fdcb12d25e5dee1811874d)
, 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
![](http://7xjob4.com1.z0.glb.clouddn.com/a3f26928d95d28835491ed15ded8b273)
is the floor function and that
![](http://7xjob4.com1.z0.glb.clouddn.com/ebc18351569dbb8b6f8b3fc997543dd1)
for some real number
R is the largest integer that is
![](http://7xjob4.com1.z0.glb.clouddn.com/5df55c21488b6476974f89804dda36c2)
.
Here are Wally's notes on the subject:
Let
![](http://7xjob4.com1.z0.glb.clouddn.com/1c55915c1505c7110888c1314ec0e35e)
consist of positive integers
![](http://7xjob4.com1.z0.glb.clouddn.com/40db9334f1b76008975bf26099b0975e)
. The problem is to find the smallest positive integer
C such that
![](http://7xjob4.com1.z0.glb.clouddn.com/5fc344ab96f4ae7aff6d7976ca6a5f0e)
for all
![](http://7xjob4.com1.z0.glb.clouddn.com/499a41a3f07c8b7612f47bf3be681d91)
.
C must be a multiple of at least one element of W.
If some
![](http://7xjob4.com1.z0.glb.clouddn.com/9b9b1f75b811aeb33aa828f4fe27da57)
for all
![](http://7xjob4.com1.z0.glb.clouddn.com/9bb5069d7ca519da67f4f93edf269e3b)
,
then the next largest C that could resolve the conflict is at least
![](http://7xjob4.com1.z0.glb.clouddn.com/2e129c97c9c44302602adcd16fa73503)
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,
![](http://7xjob4.com1.z0.glb.clouddn.com/66a2d2e7c36bb4c3cf397e0f0865b9ee)
, `z' to be 26. Use 5 bits for each letter (shift left by 5 or multiply by 32). Thus `a'
= 1, `bz' =
![](http://7xjob4.com1.z0.glb.clouddn.com/6958300d904a677f33e64ec380f2f8af)
.
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.
这道题真的有够难懂
复制以下别人的翻译
有一个完美哈希函数,其中
![](http://uva.onlinejudge.org/external/1/188img1.gif)
,C是一个正数, 也就是你要找的那个数(结果要输出这个数)。w是由一个单词转换得到的数字,例如 `a'
= 1, `bz' =
![](http://uva.onlinejudge.org/external/1/188img13.gif)
,
可以把它看成是32进制的转换。 n其实就是代表的是一行中的单词的个数。
然后怎样求出C呢?
首先对于
![](http://uva.onlinejudge.org/external/1/188img5.gif)
,
![](http://uva.onlinejudge.org/external/1/188img6.gif)
.是由一行中的各个单词转换而来的,然后题目说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集合已经排好序了:
![](http://uva.onlinejudge.org/external/1/188img6.gif)
)。
对于C,要让它符合条件
![](http://uva.onlinejudge.org/external/1/188img7.gif)
for
all
![](http://uva.onlinejudge.org/external/1/188img8.gif)
. 所以要用一个两层for循环来判断。
如果不符合的话,就让C等于:
![](http://uva.onlinejudge.org/external/1/188img11.gif)
一直到找到符合的条件C为止,答案就出来了。
左移5位代码相当于转换成32位
w[i] = (w[i] << 5) + word[i][j] - 'a' + 1;
如果C改变了,就应该重新检验
所以用递归
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
SubmitStatusPracticeUVA
188
Appoint description:
Description
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
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 alwaysbe 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
这道题真的有够难懂
复制以下别人的翻译
有一个完美哈希函数,其中
![](http://uva.onlinejudge.org/external/1/188img1.gif)
,C是一个正数, 也就是你要找的那个数(结果要输出这个数)。w是由一个单词转换得到的数字,例如 `a'
= 1, `bz' =
![](http://uva.onlinejudge.org/external/1/188img13.gif)
,
可以把它看成是32进制的转换。 n其实就是代表的是一行中的单词的个数。
然后怎样求出C呢?
首先对于
![](http://uva.onlinejudge.org/external/1/188img5.gif)
,
![](http://uva.onlinejudge.org/external/1/188img6.gif)
.是由一行中的各个单词转换而来的,然后题目说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集合已经排好序了:
![](http://uva.onlinejudge.org/external/1/188img6.gif)
)。
对于C,要让它符合条件
![](http://uva.onlinejudge.org/external/1/188img7.gif)
for
all
![](http://uva.onlinejudge.org/external/1/188img8.gif)
. 所以要用一个两层for循环来判断。
如果不符合的话,就让C等于:
![](http://uva.onlinejudge.org/external/1/188img11.gif)
一直到找到符合的条件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; }
相关文章推荐
- 静态分派和动态分派
- CocoaPods安装和使用教程
- mac 搭建android studio 开发环境
- Android--WebView
- linux64需要增加的依赖库
- Java [Leetcode 226]Invert Binary Tree
- libc、glibc和glib的关系
- Android学习之动画实例
- Dubbo分布式服务框架入门(附工程)
- 浅谈如何避免service挂掉
- spring MVC 中AJAX 请求 get 与 post
- 程序的测试与跟踪
- 彻底删除MySQL服务 删除服务:sc delete mysql//这里的mysql是你要删除的服务名在
- 给定一个节点为头节点,从尾到头反过来打印出每个节点的值
- Dubbo分布式服务框架入门(附工程)
- Android中如何实现蓝牙的配对与连接
- mave 笔记
- 读取一个txt文档中的内容
- WebDev.WebServer.exe,IIS ,IIS Express
- U3D 收藏一个飞机随机运动的方法