当前位置:网站首页>7-4 hash table search (PTA program design)
7-4 hash table search (PTA program design)
2022-07-06 13:56:00 【Programming Lindaiyu】
Set hash table a[18], The hash function is hask(k)=k%17, Use the open address method to solve conflicts hi=(h0+di)%m. Square detection method is used in case of conflict , Use incremental sequence di=i* i. Calculate the input sequence ( value >=0) Corresponding hash address and search , If there is this element , Then output hash address , If there is no such element , The output not found. And output the search times ( The number of entries will not exceed 15 individual )
Input format :
The first line is to input the number ;
The input value corresponding to the second line , Space off ;
The third line is the element to be found , The first 1 Number is the number of search elements , The following is the search element
Output format :
The first row outputs the hash address of the input sequence in turn , Separated by a space ;
The second line begins to output the hash address of the search element , Each element occupies one line , Each row corresponds to a value and its hash address , Space separates the middle ( namely pos There is a space before and after ), If there is no such element , The output not found.
sample input :
5
141 73 95 112 56
7 5 73 95 141 112 56 18
sample output :
5 6 10 11 9
5 not found,try 4
73 pos:6,try 2
95 pos:10,try 1
141 pos:5,try 1
112 pos:11,try 2
56 pos:9,try 3
18 not found,try 1
Code (Python):
list1=[] # Saved number
list2=[] # Save with hash table list1 The number in
list3=[] # Looking for elements , The first 1 Number is the number of search elements , The following is the search element , That is, input the third line of data in the sample
ct=[] # Deposit try The number of times
for i in range(18): # If there is no initial value assigned here , The list is empty. , An error will be reported below list index out of range
list2.append(0) # If you write directly here list2[i]=0 You're going to report a mistake list assignment index out of range, In one case, the subscript is out of bounds , Another case is that the list is empty
n=int(input()) ## How many numbers are there
#map() The function is to func Act on seq Every element in , And take the results of all calls as a list return .
# Although the result is list return , But it is still encouraged to add list(), Otherwise, there may be errors
list1=list(map(int,input().split())) # This row assigns a row of numbers separated by spaces to the list
for i in range(0,n): # Make exist list1 Each number in is stored in the hash table as required list2
d=1 # Square detection method is used in case of conflict , Use incremental sequence di=i*i, send d At the beginning of 1
j=list1[i]%17 # The hash function is hask(k)=k%17
if list2[j]==0: #list It's empty , Use in this case list[0] It's a mistake
list2[j]=list1[i] # That is, when the position of the hash table has not been saved list1 Get the number of hours , Save it in the hash table
ct.append(d) #try Once , take try Value added to the list ct in
else: # Otherwise, there are already in the hash table list1 The value in , Hash collision
while(list2[j]!=0): # Use the open address method to solve conflicts hi=(h0+di)%m. Square detection method is used in case of conflict , Use incremental sequence di=i*i
j=(list1[i]%17+d*d)%18 # Use the address before the conflict plus the increment to get the remainder 18, Otherwise, it will cross the border
d+=1 #try It's worth adding 1
ct.append(d) # take try Value added to the list ct in
list2[j]=list1[i] # take list1 The value in the list is stored in the hash table
print(j,end=' ') # Output the hash address of the input sequence in turn , That is, the number of the first line of output
print() # Output carriage return
list3=list(map(int,input().split())) # Looking for elements , The first 1 Number is the number of search elements , The following is the search element
for i in range(1,len(list3)): # In fact, it is from the first 2 Elements start with , But the subscript is 1
if list3[i] in list1: # First, check whether the number you want to find is in list1 in
pos1=list1.index(list3[i]) # This number is in the list 1 Subscript in , The purpose is to output with its subscript ct In the list try frequency
pos2=list2.index(list3[i]) # This number is in the list 2 Subscript in , The purpose is to output its position
print("{} pos:{},try {}".format(list3[i],pos2,ct[pos1])) # Output as required
else: # Otherwise, the number you are looking for is not list1 in
m=list3[i]%17 # But also find it try frequency
d=1 #try The number starts with 1
while list2[m]!=0: # If it should have been list2 Position in is not 0 Words , It indicates that it may encounter hash conflicts , Move on to the next place it should be
m=(list3[i]%17+d*d)%18 # This must be divided by 18 Remainder , Otherwise you cross the line
d+=1 # Every judgment d Add 1
print("{} not found,try {}".format(list3[i],d)) # Output as required
Ideas : This question can be divided into two steps , First step , Store the data in the hash table ; The second step , Start to find the specified element in the hash table and output the address and number of attempts in the hash table . One difficulty here is the number of attempts , You can read my notes above to understand .
The above program gives more detailed comments , For novice Xiaobai's reference . The idea of program design or code implementation is not optimal , You are welcome to correct your mistakes or give better ideas .
I am a rookie who wants to be Kunpeng , Everyone's encouragement is my driving force , Welcome to like collection comments !
边栏推荐
- 强化学习基础记录
- 重载和重写的区别
- 强化學習基礎記錄
- [the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
- 实验七 常用类的使用
- Differences among fianl, finally, and finalize
- MySQL事务及实现原理全面总结,再也不用担心面试
- About the parental delegation mechanism and the process of class loading
- Record a penetration of the cat shed from outside to inside. Library operation extraction flag
- Strengthen basic learning records
猜你喜欢
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
MySQL事务及实现原理全面总结,再也不用担心面试
HackMyvm靶机系列(6)-videoclub
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
Yugu p1012 spelling +p1019 word Solitaire (string)
UGUI—Text
SRC挖掘思路及方法
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
SRC mining ideas and methods
FAQs and answers to the imitation Niuke technology blog project (III)
随机推荐
String abc = new String(“abc“),到底创建了几个对象
【MySQL数据库的学习】
7-15 h0161. 求最大公约数和最小公倍数(PTA程序设计)
2022 Teddy cup data mining challenge question C idea and post game summary
[modern Chinese history] Chapter V test
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
Brief introduction to XHR - basic use of XHR
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
Analysis of penetration test learning and actual combat stage
Nuxtjs快速上手(Nuxt2)
3. Input and output functions (printf, scanf, getchar and putchar)
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
撲克牌遊戲程序——人機對抗
5月27日杂谈
Differences among fianl, finally, and finalize
Callback function ----------- callback
Miscellaneous talk on May 27
仿牛客技术博客项目常见问题及解答(一)