7-43 字符串关键字的散列映射 (25 分)

news/2024/7/23 17:42:33

 7-43 字符串关键字的散列映射 (25 分)

给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位;再用除留余数法将整数映射到长度为P的散列表中。例如将字符串AZDEG插入长度为1009的散列表中,我们首先将26个大写英文字母顺序映射到整数0~25;再通过移位将其映射为3×32​2​​+4×32+6=3206;然后根据表长得到,即是该字符串的散列映射位置。

发生冲突时请用平方探测法解决。

输入格式:

输入第一行首先给出两个正整数N(≤500)和P(≥2N的最小素数),分别为待插入的关键字总数、以及散列表的长度。第二行给出N个字符串关键字,每个长度不超过8位,其间以空格分隔。

输出格式:

在一行内输出每个字符串关键字在散列表中的位置。数字间以空格分隔,但行末尾不得有多余空格。

输入样例1:

4 11
HELLO ANNK ZOE LOLI

输出样例1:

3 10 4 0

输入样例2:

6 11
LLO ANNA NNK ZOJ INNK AAA

输出样例2:

3 0 10 9 6 1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
using namespace std;
#define maxn 1000007
#define INF 0xffffff
#define mod = 1e6+7;

map<string, int> mp;
int vis[maxn] = {0};

int main() {
    string s;
    int n, m;
    cin>>n>>m;
    for(int i = 1; i <= n; ++i) {
        cin >> s; 
        int len = s.size();
        int num = 0;
        if(len == 1) {
            num += (s[0]-'A');
        }
        else if(len == 2) {
            num = 32*(s[0]-'A') + (s[1]-'A');
        }
        else {
            for(int j = 3; j >= 1; --j) {
                int pos = len - j;
                num = num * 32 + s[pos] - 'A';
            }
        }
        num %= m;
        if(mp[s] == 0 && vis[num]) {
            for(int t = 1; t < maxn; ++t) {           //平方探测处理冲突
                if(!vis[(num+t*t)%m]) {
                    num = (num+t*t)%m;
                    vis[num] = true;
                    mp[s] = num;
                    cout << num;
                    break;
                }
                else if(!vis[(num-t*t+m)%m]) {
                    num = (num-t*t+m)%m;
                    vis[num] = true;
                    mp[s] = num;
                    cout << num;
                    break;
                }
            }
        }
        else {
            num %= m;
            mp[s] = num;
            cout << num;
            vis[num] = true;
        }
        if(i < n) 
            cout << " ";
        else 
            cout << endl;
    }
    return 0;
}

 


http://www.niftyadmin.cn/n/4610373.html

相关文章

Node.js 开发相关

2019独角兽企业重金招聘Python工程师标准>>> 全局安装默认存放路径npm install express -g C:\Users\Administrator\AppData\Roaming\npm转载于:https://my.oschina.net/u/1179666/blog/1511162

P1216 [USACO1.5][IOI1994]数字三角形 Number Triangles【DP、数塔问题】

题目描述 观察下面的数字金字塔。 写一个程序来查找从最高点到底部任意处结束的路径&#xff0c;使路径经过数字的和最大。每一步可以走到左下方的点也可以到达右下方的点。 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 在上面的样例中,从 7 \to 3 \to 8 \to 7 \to …

Linux大文件分割split和合并cat使用方法

本文主要介绍linux下两个命令&#xff1a;split和cat。其中&#xff0c;相信大家都熟悉cat命令&#xff0c;一般用来查看一个文件的内容&#xff0c;但是它还其它的功能&#xff0c;比如这里要介绍的文件合并功能&#xff0c;它可把多个文件内容合并到一个文件中。从split词义不…

RxJava 机制

韩梦飞沙 韩亚飞 313134555qq.com yue31313 han_meng_fei_sha rxjava 是 以 响应式 编程思想 编程的 java类库转载于:https://www.cnblogs.com/yue31313/p/7375552.html

vim 配色方案

2019独角兽企业重金招聘Python工程师标准>>> //这里有一个在线vim颜色编辑网站,也可以下载配置好的vim配色 http://bytefluent.com/vivify/ 1. GRB256 GRB256 基于 ir_black&#xff0c;感觉特别适合 Ruby on Rails 的应用开发。 网址: GitHub 作者: Gary Bernhardt…

写一个类的时候一些注意点

1 java 定义类的时候&#xff0c;属性的类型最好使用自动装配的类型 比如 错误的做法&#xff1a; public class user{ private int id; 正确的做法 public class user{ private Integer id; 原因&#xff1a;第一种做法&#xff0c;表单提交的时候,且没有对id属性的输入…

SimpleConsumer(翻译)

2019独角兽企业重金招聘Python工程师标准>>> Using SimpleConsumer Why use SimpleConsumer? The main reason to use a SimpleConsumer implementation is you want greater control over partition consumption than Consumer Groups give you. 使用一个SimpleCo…

mysql 5.7 root忘记密码

2019独角兽企业重金招聘Python工程师标准>>> service mysqld stop mysqld_safe --skip-grant-tables & update mysql.user set authentication_stringpassword(123qwe) where userroot and Host localhost; service mysql restart set password for rootlocalh…