Poj 2503 babelfish hash/qsort + bsearch

Source: Internet
Author: User

Question: enter a dictionary and then search for words. (Enter a space as the field)
Question: Do not forget to perform string binary search.

# Include <cstring> # include <iostream> using namespace STD; # define prime 100003 struct Union {char Eng [11], fore [11];} p [100001]; struct node {int ID; bool flag;} hash [prime] [10]; int get_hash (char * Str) // I really don't know how to push this function {int h = 0; while (* Str) {H = (H <4) + * STR ++; int G = H & 0xf0000000l; If (g) H ^ = G> 24; H & = ~ G;} return H % prime;} int main () {char STR [100]; Int J, sum, Mark, index, id =-1; memset (hash, null, sizeof (hash); While (gets (STR) & STR [0]! = 0) {++ ID; j = sum = 0; sscanf (STR, "% S % s", P [ID]. eng, P [ID]. fore); Index = get_hash (P [ID]. fore); While (hash [Index] [J]. flag = true) ++ J; hash [Index] [J]. id = ID; hash [Index] [J]. flag = true;} while (scanf ("% s", STR )! = EOF) {J = mark = 0; Index = get_hash (STR); While (hash [Index] [J]. flag! = False) {If (strcmp (P [hash [Index] [J]. id]. fore, STR) = 0) {mark = true; printf ("% s \ n", P [hash [Index] [J]. id]. eng); break;} + + J;} If (! Mark) printf ("Eh \ n");} return 0 ;}

 
Learn to perform binary search.
Function Name: bsearch

Merit: Binary Search

Usage: void * bsearch (const void * Key, const void * base, size_t * nelem, size_t width, INT (* fcmp) (const void *, const *));

Syntax:

# Include <stdlib. h> void * bsearch (const void * Key, const void * Buf, size_t num, size_t size, INT (* compare) (const
Void *, const void *));

Parameter: First: The keyword to be searched. Second: the array to be searched. Third: specify the number of elements in the array. Fourth: the length of each element (in characters ). Fifth: pointer to the comparison function.

Function: The function uses the semi-lookup method to match the parameter key from the array element Buf [0] To the Buf [num-1. If the first parameter of the Compare function is smaller than the second parameter, a negative value is returned. If the value is equal to zero, a positive value is returned. Elements in the array Buf should be arranged in ascending order. The Return Value of the function bsearch () is a match. If no match is found, null is returned.

#include <cstdlib>#include <cstring>#include <iostream>using namespace std; struct node{char eng[11], fore[11];} a[100001];int cmp1 ( const void* x, const void* y ){return strcmp( ((node*)x)->fore, ((node*)y)->fore );}int cmp2 ( const void* x, const void*y ){return strcmp( (char*)x, ((node*)y)->fore );}int main(){int id = 0;char str[50];node *p;while ( gets(str) && str[0] != 0 )sscanf( str, "%s%s", a[id].eng, a[id++].fore );qsort(a,id,sizeof(node),cmp1);while ( scanf("%s",str) != EOF ){p = (node*)bsearch ( str, a, id, sizeof(node), cmp2 );if ( p )printf ( "%s\n", p->eng );elseprintf ( "eh\n" );}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.