我有一个作业,必须使用完美的哈希来哈希输入数组中的名称,并给出名称的数量。
这是我的代码:
Dictionary.h
#include <string>
#include <vector>
using namespace std;
class Dictionary {
public:
// Insert an input set of n keys to the dictionary. This method should print out the following information:
// 1. The hash functions comprising the perfect hash (both levels)
// 2. The sum of squares of the number of keys mapped to each bin of the first level hash function, and
// 3. The number of trials needed to generate each hash function.
void bulkInsert(int n, string *keys);
// Insert a key to the dictionary.
// Print out whether the insertion caused a collision in the second level hash.
// Handle collision with separate chaining.
void insert(string key);
// Remove a key from the dictionary, if it exists.
void remove(string key);
// Return whether a key is found in the dictionary.
// Print the buckets (both first and second level) accessed during the operation.
bool find(string key);
//make random matrix and print after
void makeRandom(int nR, int nC);
//format key and hash for first level, return index of hash
int firstLevelHash(string input, int nR, int nC);
//print hash
void printHash();
private:
vector<vector<int>> randomMat;
vector<vector<int>> randomMat2;
vector<vector<string>> hashTable;
};
Dictionary.cpp
#include "Dictionary.h"
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
void Dictionary::bulkInsert(int n, string *keys) {
int numRow = (ceil(log2(n)));
int numCol = 64;
int trialNum = 1;
bool repeat = true;
while ( repeat ) {
//make random matrix
makeRandom(numRow, numCol);
//check hashtable empty, clear if not
if ( !( hashTable.empty() ) ) {
hashTable.clear();
}
//create hashtable, given by 2^numRow
for ( int i = 0; i < ( 2^numRow ); i++ ) {
vector<string> row;
hashTable.push_back(row);
}
//loop through keys and hash first level
for ( int i = 0; i<n; i++) {
int ind = firstLevelHash(keys[i], numRow, numCol);
hashTable[ind].push_back(keys[i]);
}
//check condition on whether to repeat hash
int limit = 4*n;
int summation = 0;
for ( int i = 0; i < hashTable.size(); i++ ) {
summation = summation + ((hashTable[i].size())^2);
}
if ( summation < limit ) {
repeat = false;
}
//second level hashing
}
}
void Dictionary::insert(string key) {
}
void Dictionary::remove(string key) {
}
bool Dictionary::find(string key) {
return false;
}
//randomizes randomMat and prints;
void Dictionary::makeRandom(int nR, int nC) {
vector<int> row;
for ( int i = 0; i < nR; i++ ) {
row.clear();
for ( int j = 0; j < nC; j++ ) {
int random = rand()%2;
row.push_back(random);
}
randomMat.push_back(row);
}
}
//returns vector for key of length 64
int Dictionary::firstLevelHash(string input, int nR, int nC) {
//check length of input
//if less than 8, add space to front
if ( input.size() < 8 ) {
for( int p = 0; p < 8-(input.size()); p++) {
input = " " + input;
}
}
//obtain string of length 64 of bits of input
string binStr = "";
for ( char& chr : input) {
binStr += bitset<8>(chr).to_string();
}
vector<int> keyVector;
//go through string and convert each to bit
for ( int i = 0; i < binStr.size(); i++ ) {
string substr = binStr.substr(i, 1);
int val = stoi(substr);
keyVector.push_back( val );
}
vector<int> hashFinalValue;
//multiply by hash
for ( int j = 0; j < nR; j++ ) {
int hashVal = 0;
for ( int p = 0; p < nC; p++ ) {
hashVal = hashVal + (randomMat[j][p] * keyVector[p]);
}
hashVal = hashVal % 2;
hashFinalValue.push_back( hashVal );
}
//convert binary to decimal to get index
int index = 0;
int q = 1;
for ( int m = 0; m < hashFinalValue.size(); m++ ) {
index += hashFinalValue[m] * ( 2^(hashFinalValue.size()-q) );
q++;
}
return index;
}
//prints hashTable
void Dictionary::printHash() {
for ( int i = 0; i < hashTable.size(); i++ ) {
for ( int j = 0; j < hashTable[i].size(); j++ ) {
cout << hashTable[i][j];
cout << " | ";
}
cout << "\n";
}
}
my_test_dictionary.cpp
#include "Dictionary.h"
#include <iostream>
using namespace std;
int main(){
cout<<"adfad";
Dictionary dict;
string strs[] = {"Fred Astaire", "Lauren Bacall", "Brigitte Bardot", "John Belushi", "Ingmar Bergman"};
int n = 5;
dict.bulkInsert(n, strs);
dict.printHash();
return 0;
}
[当我使用g ++ Dictionary.cpp my_test_dictionary.cpp进行编译时,我立即收到“分段错误(核心已转储)”,并且main的第一行上的“ adfad”根本没有打印。
请帮助。我坏了。
您对到达main
之前程序失败的断言为假。实际上,它在这里崩溃: