poj2503 Babelfish bkdrhash+-chain Hash

Source: Internet
Author: User

Topic links

Test instructions: The given string and the corresponding string, and then the string to find the corresponding string, there is no output "eh".

Idea: Create a template.

/********************************************************* file Name:poj2503.cpp Author:kereo create time:2015 year April 12 Sunday 17:13 12 sec *********************************************************/#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <set> #include <map> #include < vector> #include <stack> #include <cmath> #include <string> #include <algorithm> using
namespace Std;
typedef long Long LL;
typedef unsigned long long ull;
const int sigma_size=26;
const int n=15;
const int MAXN=100000+50;
const int hash=100007;
const int INF=0X3FFFFFFF;
const double eps=1e-8;
const int mod=1000000000+7;
#define L (x) (x<<1) #define R (x) (x<<1|1) #define PII pair<int, int> #define MK (x, y) Make_pair (())
int edge_cnt;
int HEAD[MAXN];
Char str[n<<1],str1[n],str2[n];
	struct node{int next; Char Str1[n],str2[n];
Str1 to KEY,STR2 for Val}edge[maxn]; struct hash{void init () {EDGE_CNt=0;
	memset (head,-1,sizeof (head)); 
		} ull Bkdrhash (char *str) {ull seed=31,tmp=0;	
		int Len=strlen (str);
		for (int i=0;i<len;i++) {tmp=tmp*seed+ (str[i]-' a ') +1;		
	} return TMP;
		} void Insert (char *str1,char *str2) {int U=bkdrhash (STR2)%hash;
		for (int i=head[u];i!=-1;i=edge[i].next) {if (strcmp (edge[i].str1,str2) = = 0) return;
		} strcpy (EDGE[EDGE_CNT].STR1,STR2);
		strcpy (EDGE[EDGE_CNT].STR2,STR1); Edge[edge_cnt].next=head[u];
	head[u]=edge_cnt++;
		} void Solve (char *str) {int U=bkdrhash (str)%hash;
				for (int i=head[u];i!=-1;i=edge[i].next) {if (strcmp (str,edge[i].str1) = = 0) {printf ("%s\n", EDGE[I].STR2);
			return;
	}} printf ("eh\n");
}}hash;
	int main () {//freopen ("In.txt", "R", stdin);
	Hash.init ();
	int C;
		while ((C=getchar ())!=eof) {int len1=0,len2=0;
		if (c< ' a ' | | | c> ' z ') break;
		Str1[len1++]=c;
		while ((C=getchar ())!=eof && c>= ' a ' && c<= ' z ') str1[len1++]=c; while ((C=getchar ())!=eof && C>= ' A ' && c<= ' z ') str2[len2++]=c;
	Hash.insert (STR1,STR2);
	} while (~SCANF ("%s", str)) {hash.solve (str);
} 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.