Source:

PAT A1078 Hashing (25 分)

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的更多相关文章

  1. [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)

    局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...

  2. Consistent hashing —— 一致性哈希

    原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...

  3. 一致性 hash 算法( consistent hashing )a

    一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...

  4. PTA Hashing

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  5. PAT1078 Hashing

    11-散列2 Hashing   (25分) The task of this problem is simple: insert a sequence of distinct positive in ...

  6. Feature hashing相关 - 1

    考虑典型的文本分类,一个经典的方法就是     分词,扫描所有特征,建立特征词典 重新扫描所有特征,利用特征词典将特征映射到特征空间编号 得到特征向量 学习参数 w 存储学习参数 w , 存储特征映射 ...

  7. 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 ...

  8. Indexing and Hashing

    DATABASE SYSTEM CONCEPTS, SIXTH EDITION11.1 Basic ConceptsAn index for a file in a database system wo ...

  9. 用单分子测序(single-molecule sequencing)和局部敏感哈希(locality-sensitive hashing)来组装大型基因组

    Assembling large genomes with single-molecule sequencing and locality-sensitive hashing 好好读读,算法系列的好文 ...

随机推荐

  1. 开源GIS软件 3

    OpenWLANMap 与 OpenStreetMap 项目类似,OpenWLANMap 将变成一个开源的替代产品,提供 WLANs 的数据库.定位信息等. 开源排水管网GIS系统 udpnGIS 邢 ...

  2. Android图片异步载入框架Android-Universal-Image-Loader

    Android-Universal-Image-Loader是一个图片异步载入,缓存和显示的框架.这个框架已经被非常多开发人员所使用,是最经常使用的几个Android开源项目之中的一个,主流的应用,随 ...

  3. 软件测试之怎么避免Bug漏测?

    一.对需求评审阶段,对业务需求细节理解不明确,未深入挖掘隐含拓展需求 改进措施 需求评审前,我们应该先仔细阅读prd及交互文档,先形成自己对产品的思考,通过脑图的方式列出对产品设计的疑问点,从用户或者 ...

  4. 用Radeon RAMDisk在Windows 10中创建关机或重新启动不消失的内存虚拟盘

    之前用ImDisk创建的内存虚拟盘每次关机或重新启动后就会消失,想要开机自己主动创建内存虚拟盘尽管能够用批处理来实现,但还是有点不爽.下载试用了Radeon RAMDisk(Radeon_RAMDis ...

  5. Cocos2d-x飞机大战教程笔记

    咳咳~跟着大神的教程学做Cocos2d-x的飞机大战...鉴于我是那种跟着教程都会出非常多错的人,所以还是一路跟着做些笔记比較好.并且因为是用课余时间,所以仅仅能断断续续地做,写下来也好让自己别忘记~ ...

  6. 3.CCFadeOutTRTiles,部落格效果,跳动的方块特效,3D瓷砖晃动特效,破碎的3D瓷砖特效,瓷砖洗牌特效,分多行消失特效,分多列消失特效

     1 TiledGrid3D //TiledGrid3D //CCFadeOutTRTiles * action = CCFadeOutTRTiles::create(2, CCSize(20,2 ...

  7. jQuery - 点击图片加边框

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. oc45--多对象内存管理 优化

    // // main.m // Set方法的内存管理 #import <Foundation/Foundation.h> #import "Person.h" #imp ...

  9. DirectFB简介以及移植[一]

    本文转载自‘:http://blog.csdn.net/wavemcu/article/details/39251805 版权声明:本文为博主原创文章,未经博主允许不得转载. ************ ...

  10. hdu 2222(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...