这题如果用STL的map来做的话,会很简单,不过为了练手,所以我还是自己写。。。STL的map的查找时间复杂度在任何时候总是O(logN),因为map用的是红黑树,可以保证树的高度总是logN。但是这里为了简单,我就直接用一颗排序二叉树来做了。很简单的题,也不用什么注释了,唯独是输入的格式要注意一下,可以用gets和sscanf来处理。
/*******************************************************************************
# Author : Neo Fung
# Email : neosfung@gmail.com
# Last modified: 2011-09-12 11:10
# Filename: ZOJ1109 Language of FatMouse.cpp
# Description :
******************************************************************************/
// ZOJ1109 Language of FatMouse.cpp : Defines the entry point for the console application.
//
// #include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include <string>
#include <string.h>
using namespace std;
struct node
{
char key[50],value[50];
struct node *left,*right;
}*Head;
void insert(const char *elem,const char *value,struct node * & p)
{
if( p == NULL)
{
p = (struct node*)malloc(sizeof(struct node));
strcpy(p->key,elem);
strcpy(p->value,value);
p->left = p->right = NULL;
return ;
}
if( strcmp(elem,p->key)>0 )
insert(elem,value,p->right);
else
insert(elem,value,p->left);
}
void find(const char *key,struct node *&p)
{
if(p==NULL)
printf("eh\n");
else if(strcmp(key,p->key)==0)
printf("%s\n",p->value);
else if(strcmp(key,p->key)>0)
find(key,p->right);
else
find(key,p->left);
}
int main(void)
{
char str[100],key[50],value[50];
while(gets(str) && strlen(str))
{
sscanf(str,"%s%s",value,key);
insert(key,value,Head);
}
while(scanf("%s",key)!=EOF)
{
find(key,Head);
}
Head=NULL;
return 0;
}