Hdu 1880 Magic Spell Dictionary (various methods)

Source: Internet
Author: User
Tags strcmp silver light
Topic Description:One of Harry Potter's required courses in the school of witchcraft is to learn spells. It is said that there are 100000 different spells in the Wizarding world, and it is difficult for Harry to remember all of them, but in order to fight a hostile enemy, he must be able to invoke any spell needed in a critical moment, so he needs your help.

Here's a spell dictionary for you. When Harry hears a spell, your program must tell him what the spell is capable of, and when Harry needs a feature but doesn't 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, no more than 100,000 different spell entries are listed in the dictionary, each in the following format:

[Spell] corresponding function

Where the "Magic Spell" and "corresponding function" are not more than 20 and 80 of the string, the string is guaranteed not to contain characters "[" and "]", and "]" and the following string has and only one space. The last line of the dictionary ends with "@END @", which is not part of the dictionary entry.
The line after the dictionary contains a positive integer n (<=1000) followed by n test cases. Each test case takes up one row, either gives a "[spell]", or gives a "corresponding function". output: the output of each test case is one row, output the function of the magic Spell, or the magic spell corresponding to the function. If the curse is not in the dictionary, the output is "what." Sample Input:

[Expelliarmus] The disarming charm
[RICTUSEMPRA] send a jet of silver light to hit the enemy
[Tarantallegra] Contr OL 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
@END @
4
[Lumos] The
summoning Charm
[Arha]
take me to the sky
  
Sample output:
Light the Wand
Accio
what?
What?
Tip: Source:

2008 Zhejiang University computer and software engineering research life test real problem


Binary search (625ms) #include "iostream" #include "stdio.h" #include "math.h" #include "vector" #include "queue" #include "memory
. h "#include" algorithm "#include" string "using namespace std;

#define N 100010 int cnt;
	struct DIC {char a[25];
Char b[85];

}d[2][n];
	int comp1 (const void *a,const void *b) {struct DIC *a= (dic*) A;
	struct DIC *b= (dic*) B;
Return strcmp (A->a,b->a);
	int comp2 (const void *a,const void *b) {struct DIC *a= (dic*) A;
	struct DIC *b= (dic*) B;
Return strcmp (A-&GT;B,B-&GT;B);
	} void Search (char*s, int dicset,int l,int r) {int i;
	int m= (L+R) >>1;
			if (dicset==1) {while (l<=r) {m= (l+r) >>1;
				if (strcmp (s,d[1][m].b) ==0) {for (I=1;i<strlen (D[1][M].A) -1;i++) printf ("%c", D[1][m].a[i));
				cout<<endl;
			Return
			else if (strcmp (s,d[1][m].b) >0) l=m+1;
		else r=m-1;
		} puts ("What?");
	return;
			else if (dicset==0) {while (l<=r) {m= (l+r) >>1; if (strcmp (S,D[0][M].A) ==0)
			{printf ("%s\n", d[0][m].b);
			return;
			else if (strcmp (S,D[0][M].A) >0) l=m+1;
		else r=m-1;
		} puts ("What?");
	Return
	int main () {char str1[25],str2[85],str[120];
	int i,j;
	cnt=0;
		while (gets (str)) {if (strcmp (str, "@END @") ==0) a break;
		i=j=0;
		while (str[i-1]!= ']) str1[j++]=str[i++];

		Str1[j]= ' ";
		j=0,i++;
		while (I<strlen (str)) str2[j++]=str[i++];

		Str2[j]= ' ";
		strcpy (D[1][CNT].A,STR1);
		strcpy (D[1][CNT].B,STR2);
		strcpy (D[0][CNT].A,STR1);
		strcpy (D[0][CNT].B,STR2);
	cnt++;
	} qsort (D[0],cnt,sizeof (d[0][0)), COMP1);

	Qsort (D[1],cnt,sizeof (d[1][0]), COMP2);
	int n;
	scanf ("%d", &n);
	GetChar ();
		while (n--) {int dic_index=1;
		Gets (STR2);
		if (str2[0]== ' [') dic_index=0;
	Search (str2,dic_index,0,cnt-1); }
}

#include "iostream"
#include "map"
#include "string"
using namespace std;
map<string,string>dic;
Char a[30],b[100];
string a1,b1;
Char c[130];

int main ()
{while
    (gets (c) &&c[0]!= ' @ ')
    {
        int i=1,x=1;
        A[0]= ' [;
        while (c[i-1]!= '])
        {
            a[x++]=c[i++];
        }
        A[x]= ' ";
        i++;
        x=0;
        while (c[i]!= ' ")
        {
            b[x++]=c[i++];
        }
        B[x]= ' ";
        A1=a;
        b1=b;
        DIC[A1]=B1;
        dic[b1]=a1;
    }
    int n;
    scanf ("%d", &n);
    Gets (a);
    while (n--)
    {
        gets (a);
        A1=a;
        Map<string,string>::iterator L1=dic.find (A1);
        if (L1 = = Dic.end ())
            cout<< "What?" <<endl;
        else
        {
            b1=l1->second;
            if (b1[0]== ' [')
                B1=b1.substr (1,b1.length ()-2);
            cout<<b1<<endl;}}}


ac-violence 2187ms) #include "iostream" #include "stdio.h" #include "math.h" #include "vector" #include "queue" #include "Memor
Y.h "#include" algorithm "#include" string "using namespace std;

#define N 200010 #define M ' z '-' [' +10 char dic[2][n][81]; int cnt;
	int Search (char* s,int dicset) {int i;
	for (i=0;i<cnt;i++) if (strcmp (s,dic[dicset][i)) ==0) return i;
if (i==cnt) return-1;
	int main () {char str1[21],str2[81],str[120];
	int i,j;
	cnt=0;
		while (gets (str)) {if (strcmp (str, "@END @") ==0) a break;
		i=j=0;
		while (str[i-1]!= ']) str1[j++]=str[i++];

		Str1[j]= ' ";
		j=0,i++;
		while (I<strlen (str)) str2[j++]=str[i++];

		Str2[j]= ' ";
		strcpy (DIC[0][CNT],STR1);
		strcpy (DIC[1][CNT],STR2);
	cnt++;
	} int n;
	scanf ("%d", &n);
	GetChar ();
		while (n--) {int dic_index=1;
		Gets (STR2);
		if (str2[0]== ' [') dic_index=0;
		int Index=search (STR2,DIC_INDEX);
		if (Index==-1) puts ("What?");
			else {if (dic_index==0) printf ("%s\n", Dic[1][index]); ElSe {for (I=1;i<strlen (Dic[0][index]) -1;i++) printf ("%c", Dic[0][index][i));
			cout<<endl; }
		}
	}
}



MLE CODE #include "iostream" #include "stdio.h" #include "math.h" #include "vector" #include "queue" #include "memory.h
"#include" algorithm "#include" string "using namespace std;

#define N 200010 #define M ' z '-' [' +10 char dic[n][81]; int cnt;
	struct Trie {struct trie* node[m];
	int index;
		Trie () {for (int i=0;i<m;i++) node[i]=null;
	Index=-1;

}
};
	void Insert (Trie *root,char* s,char *stodic,int len) {Trie *r=root;
		for (int i=0;i<len;i++) {int c=s[i]-';
		if (s[i]== ') c= ' z ' +1-';
		if (r->node[c]==null) r->node[c]=new Trie;
	r=r->node[c];
	} strcpy (Dic[cnt],stodic);
r->index=cnt++;
	int main () {char str1[21],str2[81];
	int i,j;
	cnt=0;
	Trie *root,*r;
	Root=new Trie;
		while (scanf ("%s", str1) &&strcmp (str1, "@END @")) {GetChar ();
		Gets (STR2);
		Insert (Root,str1,str2,strlen (str1));
	Insert (Root,str2,str1,strlen (str2));
	} int n;
	scanf ("%d", &n);
	GetChar ();
		while (n--) {r=root;
		BOOL Indflag=false; Gets (STR2);
		if (str2[0]== ' [') indflag=true;
			For (I=0;i<strlen (STR2) &&r;i++) {int c=str2[i]-';
			if (str2[i]== ') c= ' z ' +1-';
		r=r->node[c];
		} if (I<strlen (STR2)) puts ("What?");
			else {if (Indflag) printf ("%s\n", Dic[r->index]);
				else {for (I=1;i<strlen (Dic[r->index]) -1;i++) printf ("%c", Dic[r->index][i));
			cout<<endl;
 }
		}
	}
}


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.