ZOJ 1109 ————————字典树基础

Language of FatMouse

Time Limit: 10 Seconds       Memory Limit: 32768 KB

We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.

Input Specification

Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output Specification

Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Output for Sample Input

cat
eh
loops
这个题目描述很简单,最后可以转变成一个字符串查询的问题,就是建立一棵"字典树“,在树上查询即可,属于很基础的应用。
有一点小技巧,就是trie的结构体,写成这样:
struct Trie{
	int find;
	char s[15];
	struct Trie *next[26];
};
当且仅当,构造完一个单词的时候,例如acm这个单词:
那么node( 'm')->find = 1;
这样查询的时候只要最后一个结点的find值为1,说明找到了这个单词,否则说明没找到.
(详细看代码)
下附代码,AC(70MS)
#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct Tree{
	int num;
	char s[15];
	struct Tree *next[27];
};
struct Tree *root;
void Initialize()
{
	root = (struct Tree*)malloc(sizeof(struct Tree));
	root->num = 0;
	for(int i = 0 ; i < 26 ; ++i)
		root->next[i] = NULL;
}
void Insert(char *s1,char *s2)//以s1为基础建立字典树 
{
	int len = strlen(s1);
	struct Tree *node,*p = root;
	
	for(int i = 0 ; i < len ; ++i){
		int pos = s1[i] - 'a';
		if(p->next[pos] == NULL){
			//printf("%c %d %c\n",s1[i],pos,pos + 'a');
			node = (struct Tree*)malloc(sizeof(struct Tree));
			for(int j = 0 ; j < 26 ; ++j){
				node->next[i] = NULL;
				node->num = 0;
			}
			p->next[pos] = node;
		}
		p = p->next[pos];
	} 
	p->num = 1;
	strcpy(p->s,s2);
}
void Find(char *s)
{
	int len = strlen(s);
	int i;
	struct Tree *p = root;
	
	for(i = 0 ; i < len ; ++i){
		int pos = s[i] - 'a';
		if(p->next[pos] != NULL)
			p = p->next[pos];
		else
			break;
	}
	if(p->num == 1)
		printf("%s\n",p->s);
	else
		printf("eh\n");
}
int main()
{
	char str[30];
	
	Initialize();
	while(gets(str)){
		if(strlen(str) == 0)
			break;
		char str1[12],str2[12];
		memset(str1,0,sizeof(str1));
		memset(str2,0,sizeof(str2));
		for(int i = 0 ; i < strlen(str) ; ++i){
			if(str[i] == ' '){
				strncpy(str1,str,i);
				str1[i] = '\0';
				strncpy(str2,str+i+1,strlen(str)-i-1);
				str2[strlen(str) - i - 1] = '\0';
				//printf("%s %s\n",str1,str2);
			}
		}
		Insert(str2,str1);
	}
	while(scanf("%s",str) != EOF)
		Find(str);
	return 0;
}

还有一种非常坑爹的做法...就是C++的STL的map,代码量很短也不用思考,就是速度很慢
(1460MS,A)
#include <iostream>
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
map<string,string> M;
int main()
{
    char a[12],b[12],str[30];
    while(gets(str))
    {
        if(strcmp(str,"")==0)break;
        int i,l=strlen(str);
        for(i=0;i<l;++i)
        {
            if(str[i]==' ')break;
        }
        strncpy(a,str,i);
        a[i]='\0';
        strncpy(b,str+i+1,l-i);
        b[l-i]='\0';
       // cout<<a<<endl<<b<<endl;
        M[b]=a;
    }

    while(cin>>b)
    {
        if(M[b]=="")
        {
            cout<<"eh"<<endl;
        }
        else
        {
            cout<<M[b]<<endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值