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 好好读读,算法系列的好文 ...
随机推荐
- Linux vim 入门 配置 及 使用初步
网上能够找到的,关于VI的教程,更是多为能吓死人的上百页说明.事实上, 从我个人的实践看,全然不须要如此夸张.要完毕最主要的编辑.仅仅要熟悉几个命令,就是把VIM用得非常好. 这里就列举一下: Esc ...
- 编程之美初赛第二场AB
题目1 : 扑克牌 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 一副不含王的扑克牌由52张牌组成,由红桃.黑桃.梅花.方块4组牌组成,每组13张不同的面值.现在给定52 ...
- Android 应用启动动画代码
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题 setContentView(R.layout.activity_main); getWin ...
- Navicat Lite 提示Connection to mysql server on 10065
Navicat Lite 提示Connection to mysql server on 10065 验证过主要是防火墙问题 [root@014136251035 zhop]# vi /etc/sys ...
- python pickle to json
ref: https://gist.github.com/Samurais/567ebca0f59c612eb977065008aad867 ''' Convert a pkl file into j ...
- nyoj--990--蚂蚁感冒(模拟)(思维题)
蚂蚁感冒 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/ ...
- python Paramiko 模块远程管理主机
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import paramiko import os, stat import sys import ope ...
- E20170911-hm
specification n. 规格; 说明书; 详述;
- bzoj3436: 小K的农场(差分约束)
3436: 小K的农场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1575 Solved: 690[Submit][Status][Discus ...
- 日期数据类型为Date ,前台传递喂String的后台处理
方法一: 在实体类里面将set方法里面将数据类型转为Date public void setBirth(String birth) { SimpleDateFormat sdf = new Simpl ...