Implement a rabin_hash method to make the following code work:
int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our filter, allocating the necessary memory*/ bloom_filter filter = bloom_init(bsz); /*compute the hash factor.*/ long long hash_factor = 1; int i; int j; for (i = 0; i < k - 1; i++) { hash_factor = mmul(hash_factor, 256); } int score = 0; for (i = 0 ; i < m / k ; i++) { /*add each length k chunk into the bloom filter.*/ bloom_add(filter, rabin_hash(qs + i * k, k)); } bloom_print(filter, PRINT_BLOOM_BITS); long long rolling_hash = rabin_hash(ts, k); for (i = 0 ; i < n - k + 1 ; i++) { /*If we get a match on the rolling hash, check that the substring exists.*/ if (bloom_query(filter, rolling_hash)) { /* iterate through each chunk doing a string compare */ for (j = 0 ; j < m / k ; j++) { /*If we find a match, increment the count.*/ if (!strncmp(qs + j * k, ts + i, k)) { score++; break; } } } /*roll the hash forward.*/ rolling_hash = madd(mmul(mdel(rolling_hash, mmul(*(ts + i), hash_factor)), 256), *(ts + k + i)); } return score; }
bloom.c (where the bloom methods come from):
bloom.c
#include "bloom.h" /* Constants for bloom filter implementation */ const int H1PRIME = 4189793; const int H2PRIME = 3296731; const int BLOOM_HASH_NUM = 10; /* The hash function used by the bloom filter */ int hash_i(int i, /* which of the BLOOM_HASH_NUM hashes to use */ long long x /* a long long value to be hashed */) { return ((x % H1PRIME) + i*(x % H2PRIME) + 1 + i*i); } /* Initialize a bloom filter by allocating a character array that can pack bsz bits. (each char represents 8 bits) Furthermore, clear all bits for the allocated character array. Hint: use the malloc and bzero library function Return value is the newly initialized bloom_filter struct.*/ bloom_filter bloom_init(int bsz /* size of bitmap to allocate in bits*/ ) { bloom_filter f; f.bsz = bsz; /* calculates the number of bytes required*/ int bytes_size; if (bsz % 8 == 0) bytes_size = bsz/8; else bytes_size = (bsz - (bsz % 8))/8 + 1; /* allocates memory for the charactera array and sets it to zero*/ f.buf = (char *) calloc(bsz , bytes_size); return f; } /* Add elm into the given bloom filter*/ void bloom_add(bloom_filter f, long long elm /* the element to be added (a RK hash value) */) { int i; int bloom_position; for (i = 0; i < BLOOM_HASH_NUM; i++) { /* Finds the required byte and bit poistion */ bloom_position = hash_i(i, elm) % f.bsz; int temp_byte = 1 << (7 - bloom_position % 8); /* Sets the requried bit to 1 using OR operation */ f.buf[bloom_position / 8] = f.buf[bloom_position / 8] | temp_byte; } return; } /* Query if elm is probably in the given bloom filter */ int bloom_query(bloom_filter f, long long elm /* the query element */ ) { int i; int bloom_position; for (i = 0; i < BLOOM_HASH_NUM; i++) { /* Finds the required byte and bit poistion */ bloom_position = hash_i(i, elm) % f.bsz; int temp_byte = 1 << (7 - bloom_position % 8); /* Checks whether the bit is 1 using AND operation*/ if (!( (f.buf[bloom_position / 8]) & temp_byte)) return 0; } return 1; } void bloom_free(bloom_filter *f) { free(f->buf); f->buf = f->bsz = 0; } /* print out the first count bits in the bloom filter */ void bloom_print(bloom_filter f, int count /* number of bits to display*/ ) { int i; assert(count % 8 == 0); for(i=0; i< (f.bsz>>3) && i < (count>>3); i++) { printf("%02x ", (unsigned char)(f.buf[i])); } printf("\n"); return; }
Below is your function
/* Produces the initial rabin hash of length len at the start of the string */ long long rabin_hash(const char *to_encode, /* the string to encode */ int len /* the length of the encoded string */ ) { long long hash = 0; int i; /* Iterate backwards over the range to encode it */ for (i = 0; i < len; i++) { hash = madd(*(to_encode + i), mmul(hash, 256)); } return hash; }
Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...
Java code efficiency: Look at the implementation of the method called intervalSums below. Design and implement a more efficient runtime algorithm for intervalSums. public static long intervalSums(intll A, intl0 B) long count = 0; for (int i = 0; i < A.|ength; i++) { for (int j = i; j < A.length; j++) { int sum = 0; for (int k = i; k <= j; k++) { sum += A[k]; count += 1; B[i][j] = sum; if (j >...
howthe output of the following 4 program segments (a) const int SIZE=8; int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8}; int index; index=0; res = values[index]; for (int j=1; j<SIZE; j++) { if (values[j] > res) { res = values[j]; index = j; cout << index << res << endl; } } cout <<...
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...
I need help implementing the following code without using filehandling and only getchar(); this is the prompt "The input for your program will be a text file containing a large amount of English. Typically, an English sentence ends with a period (aka, dot). Many years ago, when people used mechanical typewriters, the proper form was to place one space between words in a sentence, but two spaces after the period at the end of the sentence. This rule is no...
How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c; int count = 0; for(i = 0; i < a; i++){ for(j = 0;j < 9;j++){ c = fgetc(fp); if(c == '-'){ board[i][j] = 0; } else if(c >= 1 && c <= 9) printf(" %d"); else return -2; } count++; } if(count != a-1) return -1; else return 0; }12 Read...
4. Hashing and Hash Tables. You need to use the ASCII table in the last page for this question. Study the following hash functions for ASCII C strings that are at least 3-char long unsigned hash1(const char, unsigned unsigned vto]+01997 return (v % m); unsigned hash2Cconst char unsigned) unsigned v-o]k(2] 877 return 1 + (v % ( -1)); (a) Given that m-, 7, compute the hash values and fill the following table (3%) String k hash1k, ) hash2(k, 7) aph...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
Attached to this assignment as a separate document is the C++ code for a program that determines if a given string is a palindrome or not. A palindrome is a word that is spelled the same backward or forward. "bob" is an example of a palindrome. The program is sometimes a talking point during lecture, and may not fully adhere to the many bobisms I've been telling you about. In fact, it may not entirely work but that wasn't its...
Write the following function: const int MIN_SIZE = 2; const int MAX_SIZE = 8; const int UNKNOWN = 0; const int RED = 1; const int BLUE = 2; const char UNKNOWN_LETTER = '-'; const char RED_LETTER = 'X'; const char BLUE_LETTER = 'O'; const string UNKNOWN_STRING = "unknown"; const string RED_STRING = "X"; const string BLUE_STRING = "O"; /** * Requires: size <= MAX_SIZE and size is a positive even integer, * 0 <= row && row < size....