Problem description Harry Potter one of the compulsory courses in the wizarding school is to learn the curse. It is said that there are 100000 different enchantments in the Wizarding world, and it is difficult for Harry to remember it all, but in order to fight the enemy, he must be able to invoke any desired spell in a critical moment, so he needs your help.
Give you a spell dictionary. When Harry hears a spell, your program must tell him the function of the spell, and when Harry needs a function but does not know what spell to use, your program will find the appropriate spell for him. If the spell he wants is not in the dictionary, the output is "what?"
Input first lists no more than 100,000 different spell entries in the dictionary, each in the following format:
[Magic Spell] corresponding function
where "enchantments" and "corresponding functions" are strings that do not exceed 20 and 80 in length, the string guarantees that the characters "[" and "]" are not included, and that there is only one space between the "]" and the subsequent strings. The last line of the dictionary ends with "@[email protected", which does not belong to the dictionary entry.
The line after the dictionary contains a positive integer n (<=1000) followed by n test cases. Each test case takes on one line, or "[enchantments]", or "corresponding function". Output for each test case is one row, the output enchantment corresponds to the function, or the function corresponding to the enchantment. If the spell is not in the dictionary, output "what?" Sample Input[expelliarmus] The disarming Charm[rictusempra] send a jet of silver light-to-hit the Enemy[tarantallegra] Con Trol the movement of one ' s Legs[serpensortia] shoot a snake out of the end of one's wand[lumos] light the wand[obliviate] The memory Charm[expecto Patronum] Send a Patronus to the Dementors[accio] the summoning Charm@[email protected]4[lumos]th E Summoning charm[arha]take me to the sky Sample outputlight the wandacciowhat?what?
/*This problem with map will explode space, with hash will collide, randomly pick up a similar to the table of things, the hash value of the string is a point, the string is the edge, the query when the value of the hash to traverse its edge*/#include<iostream>#include<cstdio>#include<cstring>#include<map>#defineM 100010#defineMoD 100007#defineN 110#defineHS 117using namespacestd;structnode{Chars1[ A],s2[ the]; intPre;} A[M],B[M];intN,l,k,ha,cnt,heada[mod],headb[mod];CharS[n],c,q1[n],q2[n],si[n];intHash (Char*A)//Hash Value{ intret=1, l=strlen (a); for(intI=0; i<l;i++) ret= (Ret*hs%mod+a[i])%MoD; returnret%MoD;}voidInsert (Char*a,Char*B)//Build Edge{cnt++; strcpy (A[cnt].s1,a); strcpy (A[CNT].S2,B); Ha=Hash (a); A[cnt].pre=Heada[ha]; Heada[ha]=CNT; strcpy (B[cnt].s1,a); strcpy (B[CNT].S2,B); Ha=Hash (b); B[cnt].pre=Headb[ha]; Headb[ha]=CNT;}intFind1 (Char*A)//find the right side of the hash value{ha=Hash (a); for(intI=heada[ha];i;i=a[i].pre)if(!STRCMP (A[i].s1,a))returni; return 0;}intFind2 (Char*A)//find the right side of the hash value{ha=Hash (a); for(intI=headb[ha];i;i=b[i].pre)if(!STRCMP (B[i].s2,a))returni; return 0;}intMain () { while(1) {memset (Q1,0,sizeof(Q1)); memset (Q2,0,sizeof(q2)); Gets (s);if(s[0]=='@') Break; L=strlen (s); intl1=-1, l2=-1; for(intI=0; i<l;i++) { if(s[i]=='[')Continue; if(s[i]==']') {k=i+2; Break;} q1[++l1]=S[i]; } for(inti=k;i<l;i++) q2[++l2]=S[i]; Insert (Q1,Q2); } CIN>>n;c=GetChar (); for(intI=1; i<=n;i++) {gets (s); L=strlen (s);intli=-1, flag=0; for(intI=0; i<l;i++) { if(s[i]=='[') {flag=1;Continue;} if(s[i]==']') Break; si[++li]=S[i]; } if(flag) {intR=find1 (SI); !r?cout<<"What ?"<<endl:cout<<A[r].s2<<Endl; } Else { intR=Find2 (SI); !r?cout<<"What ?"<<endl:cout<<B[r].s1<<Endl; } } return 0;}
View Code
Spell Dictionary (hdu 1880)