zoukankan      html  css  js  c++  java
  • PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-43字符串关键字的散列映射 (25 分)

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

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

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

    输入格式:

    输入第一行首先给出两个正整数N(≤)和P(≥的最小素数),分别为待插入的关键字总数、以及散列表的长度。第二行给出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
    题目分析:这是也是一道散列表的基础题
      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include<stdio.h>
      3 #include<string.h>
      4 #include<malloc.h>
      5 #include<math.h>
      6 #define MAXTABLESIZE 100000
      7 
      8 typedef enum{Legitimate,Empty,Deleted}EntryType;
      9 typedef struct HashEntry Cell;
     10 struct HashEntry
     11 {
     12     EntryType Type;
     13     char Data[9];
     14 };
     15 typedef struct HblNode* HashTable;
     16 struct HblNode
     17 {
     18     int TableSize;
     19     Cell* Cells;
     20 };
     21 
     22 int NextPrime(int N)
     23 {
     24     int P = (N % 2)?N:N + 1;
     25     for (; P < MAXTABLESIZE; P += 2)
     26     {
     27         int i = (int)sqrt(P);
     28         for (; i > 2; i--)
     29             if (P % i == 0)
     30                 break;
     31         if (i == 2)
     32             break;
     33     }
     34     return P;
     35 }
     36 
     37 HashTable CreateHashTable(int N)
     38 {
     39     int TableSize = NextPrime(N);
     40     HashTable H = (HashTable)malloc(sizeof(struct HblNode));
     41     H->TableSize = TableSize;
     42     H->Cells = (Cell*)malloc(H->TableSize * sizeof(Cell));
     43     for (int i = 0; i < H->TableSize; i++)
     44     {
     45         H->Cells[i].Type = Empty;
     46         H->Cells[i].Data[0] = '';
     47     }
     48     return H;
     49 }
     50 
     51 int Hash(char Data[], int TableSize)
     52 {
     53     int L = strlen(Data);
     54     int sum = 0;
     55     if (L >= 3)
     56     {
     57         L--;
     58         for (int i = L - 2; i <= L; i++)
     59             sum = sum * 32 + Data[i] - 'A';
     60     }
     61     else
     62     {
     63         for (int i = 0; i < L; i++)
     64             sum = sum * 32 + Data[i] - 'A';
     65     }
     66     return sum%TableSize;
     67 }
     68 
     69 int Find(char Data[], HashTable H)
     70 {
     71     int NewPos, CurrentPos;
     72     NewPos = CurrentPos = Hash(Data, H->TableSize);;
     73     int CNum = 0;
     74     while (H->Cells[NewPos].Type!=Empty&&strcmp(H->Cells[NewPos].Data,Data))
     75     {
     76         if (++CNum % 2)
     77         {
     78             NewPos = CurrentPos + ((CNum + 1) / 2)*((CNum + 1) / 2);
     79             while (NewPos >= H->TableSize)
     80                 NewPos -= H->TableSize;
     81         }
     82         else
     83         {
     84             NewPos = CurrentPos - (CNum / 2) * (CNum / 2);
     85             while (NewPos < 0)
     86                 NewPos += H->TableSize;
     87         }
     88     }
     89     return NewPos;
     90 }
     91 
     92 void Insert(char Data[], HashTable H)
     93 {
     94     int Pos = Find(Data, H);
     95     if (H->Cells[Pos].Type != Legitimate)
     96     {
     97         H->Cells[Pos].Type = Legitimate;
     98         strcpy(H->Cells[Pos].Data, Data);
     99     }
    100 }
    101 
    102 int main()
    103 {
    104     int N, P;
    105     scanf("%d%d", &N, &P);
    106     HashTable H = CreateHashTable(P);
    107     char Data[9] = { 0 };
    108     for (int i = 0; i < N - 1; i++)
    109     {
    110         scanf("%s", Data);
    111         Insert(Data, H);
    112         printf("%d ", Find(Data, H));
    113     }
    114     scanf("%s", Data);
    115     Insert(Data, H);
    116     printf("%d", Find(Data, H));
    117     return 0;
    118 }
    View Code
  • 相关阅读:
    Oracle 11g SQL Fundamentals Training Introduction02
    Chapter 05Reporting Aggregated data Using the Group Functions 01
    Chapter 01Restriicting Data Using The SQL SELECT Statemnt01
    Oracle 11g SQL Fundamentals Training Introduction01
    Chapter 04Using Conversion Functions and Conditional ExpressionsConditional Expressions
    Unix时代的开创者Ken Thompson (zz.is2120.bg57iv3)
    我心目中计算机软件科学最小必读书目 (zz.is2120)
    北京将评估分时分区单双号限行 推进错时上下班 (zz)
    佳能G系列领军相机G1X
    选购单反相机的新建议——心民谈宾得K5(转)
  • 原文地址:https://www.cnblogs.com/57one/p/11686833.html
Copyright © 2011-2022 走看看