UVA 188 - Perfect Hash
来源:互联网 发布:编程数学要求高吗 编辑:程序博客网 时间:2024/05/01 01:59
题目大意:有一个完美哈希函数,其中,C是一个正数, 也就是你要找的那个数(结果要输出这个数)。w是由一个单词转换得到的数字,例如 `a' = 1, `bz' = , 可以把它看成是32进制的转换。 n其实就是代表的是一行中的单词的个数。
解题思路:首先对于, .是由一行中的各个单词转换而来的,然后题目说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为止,答案就出来了。
#include <cstdio>#include <cstring>int main() {char str[100];while (gets(str)) {int w[15] = {0}, n = 0, C = 1 << 31 - 1;for (int i = 0; i < strlen(str); i++)if (str[i] >= 'a' && str[i] <= 'z')w[n] = w[n] * 32 + str[i] - 'a' + 1;else if (str[i] != str[i + 1])n++;for (int i = 0; i <= n; i++)C = (C > w[i] ? w[i] : C);for (int i = 0; i <= n; i++)for (int j = i + 1; j <= n; j++)if (C / w[i] % (n + 1) == C / w[j] % (n + 1)) {C = (w[i] < w[j] ? (C / w[i] + 1) * w[i] : (C / w[j] + 1) * w[j]);i = j = 0;}printf("%s\n%d\n\n", str, C);}return 0;}
0 0
- Uva 188 Perfect Hash
- UVa 188 - Perfect Hash
- uva 188 - Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 Perfect Hash
- UVa 188 - Perfect Hash
- UVA 188 - Perfect Hash
- UVA - 188 Perfect Hash
- uva 188 Perfect Hash
- UVa 188 - Perfect Hash
- UVA - 188 Perfect Hash
- Uva--188 Perfect Hash
- uva 188 - Perfect Hash 模拟
- UVA - 188 Perfect Hash(hash)
- uva 188 Perfect Hash(进制转换 + 遍历)
- 188 - Perfect Hash
- Android 涂鸦最佳实践
- 第26课 如何设置绘图 以及 多线程的应用
- Linux 简介
- Vector
- UVA 10537 - The Toll! Revisited(dijstra扩展)
- UVA 188 - Perfect Hash
- 浏览器的CSS Hacks
- Cristian’s (Time Server) Algorithm
- 小试牛刀之BTrace
- IntelliJ IDEA 快捷键
- Objective-C的基本语法
- ASP.NET Web API 控制器执行过程(一)
- 好莱坞创造出了一种完爆IMAX的观影模式
- java 编程思想-java运算符--以前不太明白的