实验目的
- 掌握存放大量数据的方法(主要是姓名);
- 掌握查找大量数据的方法(主要是姓名);
- 掌握队列的定义及操作
实验内容
在每个队伍允许插队的情况下,若你在排队,有一个以上的朋友要求插队,你可以安排他们的顺序,每次一个人入队,并且如果这个入队的人发现队伍中有自己的朋友,则可以插入到这个朋友的后面,当队伍中的朋友不止一个时,这个人会排在最后一个朋友的后面。若队伍中没有朋友,则排在队伍的最后面。每一个入队的人都先进行上述判断。当队伍前面的人买到票后,依次出队。
实验要求
选择适当的散列函数和解决碰撞方法,设计并实现插入、删除和查找算法。
分析
这道实验题目主要考察的知识点是哈希表的应用。给出应用场景,但是没有指定
输入数据的样例,自由度较高,能够应用哈希算法降低解决排队问题的时间复杂度即可。题目的大概意思是这样的:一群人去排队,每个人入列并不是插入到队列的末尾,而是队列里面最后一个与他认识的人的后面。按照这个规则,计算出全部人按照不同顺序进入队列的情况下,队列最后的顺序是怎样的。
按照传统的方法(遍历队列),当数据量大时,最消耗cpu资源计算部分是找到队列中认识的人的位置:假设a认识100个同学,队列长度为10000,那么将要进行100*10000次判断,才能找到相应的位置。这只是插入一个学生。
为了减少时间的复杂度,应该避免遍历。可以采用哈希查找的方法找到学生位置。
哈希查找是通过先计算出元素的地址然后再进行查找的方法,它需要建立和维护
一组映射关系(map),本例子中,这个关系是 学生姓名——队列位置。所以在查找某学生在队列中最后一个朋友位置时,只需要变量朋友数组,每次通过上述的关系,通过朋友的名字找到其位置即可。
MyCode
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
const int hashSize = 100;
const int maxGroupNumber = 1000;
class HashTable{
private:
vector< pair<string,int> > name_index_map[hashSize];
//get hash code by a string
int gethash(string name){
int temp = 0;
for (int i = 0; i < name.length() && i < 10; i++){
temp += abs(name[i] << 3);
}
return temp % hashSize;
}
//get the idnex of a element in the vector
int getindex(vector<pair<string, int>> vec, string name){
for (int i = 0; i < vec.size(); i++){
if (vec[i].first == name) return i;
}
return -1;
}
public:
//insert a record to the map
void insert(string name, int index){
int hashcode = gethash(name);
int indexOfvec = getindex(name_index_map[hashcode], name);
if (indexOfvec == -1){ //indexOfvec smaller then 0 mean that it name have not insert yet
name_index_map[hashcode].push_back(pair<string, int>(name, index));
}
else{ //if the name already exist, then cover the old value
name_index_map[hashcode][indexOfvec].second = index;
}
}
//remove a record from the map
int remove(string name){
int hashcode = gethash(name);
int indexOfvec = getindex(name_index_map[hashcode], name);
if (indexOfvec == -1){
return -1;
}
else{
name_index_map[hashcode].erase(name_index_map[hashcode].begin() + indexOfvec);
return 1;
}
}
//get the index by student name, return -1 if don't exist
int get(string name){
int hashcode = gethash(name);
int indexOfvec = getindex(name_index_map[hashcode], name);
if (indexOfvec == -1) return -1;
return name_index_map[hashcode][indexOfvec].second;
}
};
HashTable table;
//test the working of HashTable
void testHash(){
table.insert("hello", 12);
table.insert("Word", 1234);
table.insert("deloed", 234234);
cout << table.get("hello") << endl;
cout << table.get("Word") << endl;
cout << table.get("deloed") << endl;
cout << table.get("ooo") << endl;
}
class MyQueue{
private:
int groupNumber = 0; //the number of students group in queue
vector<string> queue[maxGroupNumber];
//find the bigest index of a group of students
int findLastestIndex(vector<string> vec){
int maxn = -1;
for (int i = 0; i < vec.size(); i++){
int tmpindex = table.get(vec[i]);
maxn = max(maxn, tmpindex);
}
return maxn;
}
//push a student into the right position
void inQueue(string name, vector<string>friendList){
int lastindex = findLastestIndex(friendList);
if (lastindex == -1){ //it mean no friend in queue yet
queue[groupNumber].push_back(name);
table.insert(name, groupNumber++);
}
else{
queue[lastindex].push_back(name);
table.insert(name, lastindex);
}
}
public:
//input the studnet message and the order of student get into the queue and push then in the queue
void getData(){
int studentNumber;
cout << "Please input the number of student >";
cin >> studentNumber;
cout << "Please input student message by format : name_of_it_student the_numbers_of_his_friends the_list_of_his_friends >"<<endl;
string tmpName;
int fridendNum;
vector<string>friendList;
for (int i = 0; i < studentNumber; i++){
cin >> tmpName;
cin >> fridendNum;
friendList.clear();
for (int j = 0; j < fridendNum; j++){
string tmp;
cin >> tmp;
friendList.push_back(tmp);
}
inQueue(tmpName, friendList);
//cout << "In queue sccuueed !\n";
}
}
//printf the order of student in queue
void printfOrder(){
int i = 0;
while (queue[i].size() != 0){
for (int j = 0; j < queue[i].size(); j++){
cout << queue[i][j] << " ";
}
i++;
}
cout << endl;
}
};
MyQueue myqueue;
int main(){
myqueue.getData();
myqueue.printfOrder();
}
/* mock data
10
abord 3 box catt alive
box 1 catt
docker 1 abord
achace 3 box catt doger
doger 3 abord catt alive
buger 4 docker box doger vir
candle 3 orz apple bigd
bigge 3 bsqk bdd thik
alia 3 achaca doger box
apple 3 redmi nokia abord
ten 2 candel docker
*/
该实验探讨了如何使用哈希表高效地解决在允许插队情况下,根据朋友关系进行队伍排列的问题。通过建立姓名到位置的映射,避免了遍历队列的高时间复杂度,实现了快速查找和插入。
406

被折叠的 条评论
为什么被折叠?



