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 ;
}
最新文章
- fiddler 挂载 JS文件
- poi导出Excel报表多表头双层表头、合并单元格
- 转:V$SQL,V$SQLAREA,V$SQLTEXT
- Oracle 逐条和批量插入数据方式对比
- Unable to write inside TEMP environment path
- zollei的心动噪声探索性识别
- LXD 2.0 系列(二):安装与配置
- iOS6和iOS7代码的适配(4)——tableView
- Spring mvc 简单异常配置jsp页面
- CSS3 Media Query
- Objective C HMAC-MD5
- PHP扩展高性能日志系统SeasLog简单上手
- C# 控制台应用程序中输出彩色字体
- SQL游标在递归是的时候提示 ";游标"; 名称已经存在的问题
- SpringSocial简介
- JAVA每日一旅2
- 去掉VS2010代码中文注释的红色下划线
- NSDictionary打印编码改中文的方法
- msys2 git status显示中文文件名问题
- 《C语言程序设计基础I》秋季学习总结