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 好好读读,算法系列的好文 ...
随机推荐
- Monthly Expense POJ 二分
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- ZooKeeper常用命令行工具及使用(转)
一.服务端 bin目录下常用的脚本解释 zkCleanup:清理Zookeeper历史数据,包括食物日志文件和快照数据文件 zkCli:Zookeeper的一个简易客户端 zkEnv:设置Zookee ...
- mongodb之备份
前言 数据库的备份非常重要 备份方式 使用Mongodb自带的运维管理工具(Ops Manager) 需要单独部署,比较麻烦和复杂 通过文件系统快照 Linux需要LVM支持需要开启journal日志 ...
- MySQL:解决MySQL无法启动的问题
MySQL无法启动的原因有多种,这里是我遇到的一种情况和解决方法. 起因: 最近项目需要使用MySQL,于是想在MAC上安装一个本地的数据库,但是其实忘了已经安装过一个版本了,结果发现新的服务器怎么也 ...
- Android面试准备 第一天 第2-4例
參考:http://blog.csdn.net/lmj623565791/article/details/24015867. .假设有个100M大的文件.须要上传至server中.而serverfor ...
- 虚拟机 开发板 PC机 三者之间不能ping通的各种原因分析
这个问题事实上也相对照较简单.可是非常多网友都给我发消息说 遇到不能ping,每一个人都得回答一次确实显得心有余而力不足.如今我对遇到这几种问题给出最完整的解决方式. (说实话基本上也仅仅要这几种可能 ...
- 南昌互联网行业协会筹办者祝真和华罡团队-2014年12月江西IDC排行榜
他出自军营,拥有一身正气. 他在南昌创业,立意卓越. 从站点開始.到微营销.到线上教育,全面开花. 他在朋友圈看到不对的内容,就会即时批评. 他对朋友,又是很的和蔼可亲. 他就是南昌华罡网络创办 ...
- select readonly 不能看到其它选项解决方式
在html中是select readonly后.依旧能够下拉选择.不想做disabled添加隐藏域,以下提供两种解决方式 解决方式1: onfocus="this.defaultIndex= ...
- 哈理工2015暑假集训 zoj 2975 Kinds of Fuwas
G - Kinds of Fuwas Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- Mongodb---记一次事故故障
2014.06.19.001---故障报告 事故发生时间 事故简述 事故责任方 是否解决 19:21-20:15 IISserverD盘即将溢出 是 一.事故描写叙述: 在19:21收到警报.显示II ...