UVa 188 - Perfect Hash

2023-12-10 04:58
文章标签 hash 188 uva perfect

本文主要是介绍UVa 188 - Perfect Hash,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=124


类型: 哈希


原题:

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 formtex2html_wrap_inline63 , 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 tex2html_wrap_inline73 is the floor function and that tex2html_wrap_inline75 for some real number R is the largest integer that is tex2html_wrap_inline79 .

Here are Wally's notes on the subject:

Let tex2html_wrap_inline81 consist of positive integers tex2html_wrap_inline83 . The problem is to find the smallest positive integer C such that

tex2html_wrap_inline87 for all tex2html_wrap_inline89 .

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

If some

tex2html_wrap_inline95 for all tex2html_wrap_inline97 ,

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

displaymath61

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, tex2html_wrap_inline103 , `z' to be 26. Use 5 bits for each letter (shift left by 5 or multiply by 32). Thus `a' = 1, `bz' = tex2html_wrap_inline105 .

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
17247663a bee see dee
4427the of and to a in that is i it with for as
667241


题目大意 + 分析与总结:


真心不喜欢这种题目, 题意很难弄懂,而弄懂了之后就像切菜一样地切。


有一个完美哈希函数,其中tex2html_wrap_inline63,C是一个正数, 也就是你要找的那个数(结果要输出这个数)。w是由一个单词转换得到的数字,例如 `a' = 1, `bz' = tex2html_wrap_inline105, 可以把它看成是32进制的转换。 n其实就是代表的是一行中的单词的个数。

然后怎样求出C呢?

首先对于tex2html_wrap_inline81 tex2html_wrap_inline83 .是由一行中的各个单词转换而来的,然后题目说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集合已经排好序了:tex2html_wrap_inline83)。 

对于C,要让它符合条件tex2html_wrap_inline87 for all tex2html_wrap_inline89 . 所以要用一个两层for循环来判断。

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

displaymath61

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



代码:

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>
using namespace std;  
int n, W[15], C, ans;  inline int min(int a, int b){  return a<b?a:b;  
}  
// 递归找出符合条件的C  
void solve( ){  for(int i=0; i<n; ++i){  for(int j=i+1; j<n; ++j)if((C/W[i])%n==(C/W[j])%n){  C = min((C/W[i]+1)*W[i], (C/W[j]+1)*W[j]);   solve();  return ;  }  }     
}  int main(){   char str[200];  int i;  while(gets(str)){  n=0;    memset(W, 0, sizeof(W));  for(i=0; i<=strlen(str); ++i){  if(str[i]==' ' || str[i]=='\0'){              ++n;  while(str[i+1]==' ')++i;  // 跳过连续的空格}  else{  W[n] = (W[n]<<5)+str[i]-'a'+1;  }  }  sort(W, W+n); // 排序,让w1<w2<w3...<wnC = W[0];solve();   puts(str);  printf("%d\n", C);  puts("");  }  return 0;  
}  

 

——  生命的意义,在于赋予它意义。

 

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)

这篇关于UVa 188 - Perfect Hash的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/476071

相关文章

C语言 | Leetcode C语言题解之第188题买卖股票的最佳时机IV

题目: 题解: int maxProfit(int k, int* prices, int pricesSize) {int n = pricesSize;if (n == 0) {return 0;}k = fmin(k, n / 2);int buy[k + 1], sell[k + 1];memset(buy, 0, sizeof(buy));memset(sell, 0, size

项目总结-前端路由hash和history

项目总结-前端路由hash和history router模块 路由需要实现的功能 当浏览器地址发生变化的时候,切换页面点击浏览器后退前进的时候,网页内容发生变化刷新浏览器,网页加载当前路由对应内容 路由主要是通过监听事件,并利用js实现动态改变网页内容,有两种实现方式 hash模式:监听浏览器地址hash地址的变化,执行相应的js切换网页history模式:利用history API实现

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符。然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string

UVA - 12206 Stammering Aliens (hash)

这题其实很容易想到2分长度,关键是2分后,怎么判断出现最多次的串是否》=m次了。这里需要用到hash来处理。 对于一个字符串   H[i] =H[i+1]*131+s[i]  ;其中 H[n]=0;那么对于一个长度为L的串 ,hanh[i,l]=H[i]-H[i+L]*x^L ; 这样记录每个字符串的hash值,然后再处理起来就比较简单了。 VIEW CODE #include<

hash算法的具体解释

这个问题有点难度,不是很好说清楚. 我来做一个比喻吧.  我们有很多的小猪,每个的体重都不一样,假设体重分布比较平均(我们考虑到公斤级别),我们按照体重来分,划分成100个小猪圈.  然后把每个小猪,按照体重赶进各自的猪圈里,记录档案. 好了,如果我们要找某个小猪怎么办呢?我们需要每个猪圈,每个小猪的比对吗?  当然不需要了. 我们先看看要找的这个小猪的体重,然后就找到了对应的猪

window.location.hash常用方法

window.location.href是在js中经常见到的获取url链接的方式 而我们在一些url中却看到过类似的地址 http://www.abc.com/a/index.html#m2 其中的#m2 就是location.hash loation.hash常与锚点联系起来使用 例如: <head><script> function getAnchor(anchor_name

【每周一库】 img_hash,rust下的pHash算法库

本期的每周一库带来的是img_hash,一个rust下的pHash算法实现。 关于pHash,一般翻译为感知哈希算法,算法通过DCT离散余弦来用固定大小矩阵(一般位8 X 8)把图像像素数据转换为频率数据,然后通过二值化计算得到图像的二进制数组,最后通过计算Hamming distance来得到两张图片的相似度数据。 下面是img_hash的相关链接 github: img_hashdoc.rs

Hash散列算法解析

虽然hash算法种类很多很多。然而,由于有先例(MD5,SHA-1,ripemd都不安全),我们很难保证使用那些标准的hash算法不会导致将来的不安全。于是,自己设计一个新的保密的hash算法就成了绝佳的选择。如何设计呢?       作者认为,至少有以下3种方法: 按照普通Hash算法模式设计修改标准Hash算法利用加密算法来构造Hash算法       第一种办法设计到的东西和内容

Redis源码学习:高性能Hash表的设计与实现

哈希表(Hash)是Redis数据库的数据类型之一,理解哈希表的实现对于掌握Redis非常重要。这篇文章,从哈希冲突和哈希扩展这两个角度,来一步步讲解Redis哈希表的工作原理。 什么是哈希表? 哈希表是一种通过哈希函数将键映射到值的数据结构。简单来说,就是通过一个计算公式(哈希函数)把一个键(比如一个名字)转换成一个数组的索引,数组中的每一个元素就是一个哈希桶(也叫bucket),然后在这个

go 通过hash,查币安链交易代码

一个简单的示例代码,用于通过哈希值查询币安链交易:   package main   import (     "encoding/json"     "fmt"     "io/ioutil"     "net/http" )   func queryBinanceTransactionByHash(hash string) {     url := "https://api.binanc