PAT_A1078#Hashing
Source:
Description:
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be (where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤) and N (≤) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.
Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -
Keys:
- 素数(Prime)
- 散列(Hash)
Attention:
- 二次探测法(平方探测法)H(key)= (key+d)%m,其中d= 正负1^2,2^2,...,k^2,k<=m
- 对于数据较大的题目,可以提前打印素数表,降低时间复杂度
Code:
/*
Data: 2019-05-13 20:16:33
Problem: PAT_A1078#Hashing
AC: 32:51 题目大意:
哈希表中插入一些不同的正整数,输出其插入的位置
哈希函数:H(key) = key%T,T为不小于MAX_SIZE的最小Prime
冲突处理:二次探测法 输入:给出M表长,N组输入
输出:给出插入位置,从0开始,无法插入打印“-”
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e4+;
int isPrime[M]; void Euler()
{
fill(isPrime,isPrime+M,);
isPrime[]=;
isPrime[]=;
vector<int> prime;
for(int i=; i<M; i++)
{
if(isPrime[i])
prime.push_back(i);
for(int j=; j<prime.size(); j++)
{
if(i*prime[j] > M)
break;
isPrime[i*prime[j]]=;
if(i%prime[j]==)
break;
}
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE Euler();
int n,m,x;
scanf("%d%d",&m,&n);
while(!isPrime[m])
m++;
int h[M]={};
for(int i=; i<n; i++)
{
scanf("%d", &x);
if(i!=)printf(" ");
int k=;
while(h[(x+k*k)%m]== && k<m)
k++;
if(h[(x+k*k)%m]==)
{
printf("%d",(x+k*k)%m);
h[(x+k*k)%m]=;
}
else
printf("-");
} return ;
}
优化:
#include<cstdio>
const int M=1e5; bool IsPrime(int n)
{
if(n== || n==)
return false;
for(int i=; i*i<=n; i++)
if(n%i==)
return false;
return true;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,m,x,h[M]={},prob[M]={};
scanf("%d%d", &m,&n);
while(!IsPrime(m))
m++;
for(int i=; i<n; i++)
{
scanf("%d", &x);
for(int j=prob[x%m]; j<=m; j++)
{
if(h[(x+j*j)%m]==)
{
h[(x+j*j)%m]=;
prob[x%m]=j+;
printf("%d%c", (x+j*j)%m,i==n-?'\n':' ');
x=-;
break;
}
}
if(x!=-)
printf("-%c", i==n-?'\n':' ');
} return ;
}
PAT_A1078#Hashing的更多相关文章
- [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)
局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...
- Consistent hashing —— 一致性哈希
原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...
- 一致性 hash 算法( consistent hashing )a
一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...
- PTA Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT1078 Hashing
11-散列2 Hashing (25分) The task of this problem is simple: insert a sequence of distinct positive in ...
- Feature hashing相关 - 1
考虑典型的文本分类,一个经典的方法就是 分词,扫描所有特征,建立特征词典 重新扫描所有特征,利用特征词典将特征映射到特征空间编号 得到特征向量 学习参数 w 存储学习参数 w , 存储特征映射 ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing
H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...
- Indexing and Hashing
DATABASE SYSTEM CONCEPTS, SIXTH EDITION11.1 Basic ConceptsAn index for a file in a database system wo ...
- 用单分子测序(single-molecule sequencing)和局部敏感哈希(locality-sensitive hashing)来组装大型基因组
Assembling large genomes with single-molecule sequencing and locality-sensitive hashing 好好读读,算法系列的好文 ...
随机推荐
- ZooKeeper之初识
它是什么 俗称动物管理员,它使用java开发,开源,接口简单,高效,稳定的分布式系统,为其它分布式系统提供协调服务 为什么会存在? 开发分布式系统跟单机上做开发完全不同,碰到的问题完全不同,开发分布式 ...
- [转]十五天精通WCF——第三天 client如何知道server提供的功能清单
通常我们去大保健的时候,都会找姑娘问一下这里能提供什么服务,什么价格,这时候可能姑娘会跟你口述一些服务或者提供一份服务清单,这样的话大 家就可以做到童嫂无欺,这样一份活生生的例子,在wcf中同样是一 ...
- CF #329 C
C题我还以为是拉格朗日插值... 其实可以想象到,必须有这样一个函数,经过某一点时,其它圆相关的函数要为0. 于是,可以构造这样的一个函数,对于x有 (x/2)*(1-abs(t-i)+abs(1-a ...
- 快速傅立叶变换&HDU 1402
参考http://www.cnblogs.com/v-July-v/archive/2011/08/13/2214132.html <算导> 那么,更快速的多项式乘法就依赖于能否把一个系数 ...
- 从Eclipse到Android Studio:Android项目怎样进行迁移
一開始我们学习Android开发.基本上都是从Eclipse上開始的,随着Google推出Android Studio,这一情况慢慢有了改变.未来非常长一段时间将会呈现Eclipse和AS相互存在的情 ...
- 查看scn headroom变化趋势的几种方法
查看scn headroom变化趋势的几种方法 scn headroom问题,本文不做解释. 本文为自己的总结,脚本来自于oracle sr技术project师. 转载请注明出处http://blog ...
- SIPp web frontend(2)
SIP VoIP 測试交流群: 323827101 欢迎大家转载.为保留作者成果,转载请注明出处.http://blog.csdn.net/netluoriver,有些文件在资源中也能够下载.假设你没 ...
- poj 1061(扩展欧几里得定理求不定方程)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...
- 线性回归(最小二乘法、批量梯度下降法、随机梯度下降法、局部加权线性回归) C++
We turn next to the task of finding a weight vector w which minimizes the chosen function E(w). Beca ...
- 第2课 Git配置文件的妙用
2-1 "git config" 指令的用法 文件夹中".git"子文件夹内的config文件的优先权>登录账号的home directory中的.gi ...