Time Limit: 8000/5000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 9381 Accepted
Submission(s): 2405
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 using namespace std; 7 8 const int MAX = 100007; 9 bool Hash1[MAX]; 10 bool Hash2[MAX]; 11 int num1[MAX],num2[MAX]; 12 int val1[MAX],val2[MAX]; 13 char xx1[100002][22];int xlen1,cur; 14 char xx2[100002][82];int xlen2; 15 16 void Insert(int x,bool *hash,int *num,int *val,int len) 17 { 18 int k=x%MAX; 19 while(hash[k]==true && num[k]!=x) 20 { 21 k++; 22 if(k==MAX) k=k-MAX; 23 } 24 if(hash[k]==false) 25 { 26 hash[k]=true; 27 num[k]=x; 28 val[k]=len; 29 } 30 } 31 bool found(int x,bool *hash,int *num,int *val) 32 { 33 int k=x%MAX; 34 while(hash[k]==true && num[k]!=x) 35 { 36 k++; 37 if(k==MAX) k=k-MAX; 38 } 39 if(num[k]==x) 40 { 41 cur=val[k]; 42 return true; 43 } 44 return false; 45 } 46 // ELF Hash Function 47 unsigned int ELFHash(char *str) 48 { 49 unsigned int hash = 0; 50 unsigned int x = 0; 51 while (*str) 52 { 53 hash = (hash << 4) + (*str++); 54 if ((x = hash & 0xF0000000L) != 0) 55 { 56 hash ^= (x >> 24); 57 hash &= ~x; 58 } 59 } 60 return (hash & 0x7FFFFFFF); 61 } 62 int main() 63 { 64 char c[150],b[20]; 65 int i,j,k,n,m; 66 while(gets(c)) 67 { 68 xlen1=-1; 69 xlen2=-1; 70 memset(Hash1,false,sizeof(Hash1)); 71 memset(Hash2,false,sizeof(Hash2)); 72 memset(num1,-1,sizeof(num1)); 73 memset(num2,-1,sizeof(num2)); 74 75 while(strcmp(c,"@END@")!=0) 76 { 77 n=strlen(c); 78 for(i=1,j=0;i<n;i++) 79 { 80 b[j++]=c[i]; 81 if(c[i]==‘]‘) 82 { 83 b[--j]=‘\0‘; 84 k=ELFHash(b); 85 xlen1++; 86 Insert(k,Hash1,num1,val1,xlen1); 87 strcpy(xx1[xlen1],b); 88 break; 89 } 90 } 91 k=ELFHash(c+i+2); 92 xlen2++; 93 Insert(k,Hash2,num2,val2,xlen2); 94 strcpy(xx2[xlen2],c+i+2); 95 gets(c); 96 } 97 scanf("%d",&m); 98 getchar(); 99 while(m--) 100 { 101 gets(c); 102 n=strlen(c); 103 if(c[0]==‘[‘) 104 { 105 c[n-1]=‘\0‘; 106 k=ELFHash(c+1); 107 if( found(k,Hash1,num1,val1) ) 108 printf("%s\n",xx2[cur]); 109 else printf("what?\n"); 110 } 111 else 112 { 113 k=ELFHash(c); 114 if( found(k,Hash2,num2,val2) ) 115 printf("%s\n",xx1[cur]); 116 else printf("what?\n"); 117 } 118 } 119 } 120 return 0; 121 }
hdu 1880 魔咒词典 (字符串哈希),布布扣,bubuko.com
原文:http://www.cnblogs.com/tom987690183/p/3597143.html