ZOJ 1109 Language of FatMouse (字典树)

这字典树敲了好多遍了...hehe

用C提交CE了

只好用C++提交了...A了

 

Problem Description
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

 

2010-11-22 10:36:47     Accepted     1109     C++     90     14040     Y

 

 

代码
 
      
#include < stdio.h >
#include
< stdlib.h >
#include
< string .h >

const int CHAR_LEN = 11 ; /* 单词长度 */
const int ALPH_LEN = 26 ; /* 字母个数 */
const int MAX = 22 ; /* 一行字符串的最大长度 */
const char EXIST = ' 1 ' ; /* 存在为'1', '0'反之 */
const char NON_EXIST = ' 0 ' ;
const int ZERO = 0 ;
const char FIRST_CHAR = ' a ' ;

/* 字典树建立 */
typedef
struct node {
struct node * child[ALPH_LEN];
char trans[CHAR_LEN]; /* 翻译 */
char exist; /* 判断是否存在此FatMouse单词 */
}node,
* Node;
Node root;
/* 字典树根结点 */

/* 插入FatMouse单词和相应的翻译 */
void insert( char * fatM, char * trans)
{
int i, index, len;
Node current
= NULL, newnode = NULL; /* 当前结点和新结点 */

len
= strlen( fatM );
if (ZERO == len) /* 单词长度为0, 不需要插入 */
{
return ;
}

current
= root; /* 每次插入都要从根结点开始 */
for (i = 0 ; i < len; i ++ )
{
index
= fatM[i] - FIRST_CHAR; /* 获取下标 */
if (NULL != current -> child[index]) /* 当前字符存在字典树中 */
{
current
= current -> child[index];
}
else
{
if ((newnode = (Node) calloc ( 1 , sizeof (node))) == NULL) {
printf(
" 空间分配失败!\n " );
exit(
- 1 );
}

current
-> child[index] = newnode; /* 插入新结点 */
current
= newnode; /* 修改当前结点位置 */
}
}

strcpy(current
-> trans, trans); /* 保存翻译 */
current
-> exist = EXIST; /* 此fatMouse单词存在 */
}

/* 翻译 */
void translate( const char * fatM)
{
int i, index, len;
Node current
= NULL; /* 当前结点 */

len
= strlen( fatM );
if (ZERO == len) /* 此单词为空, 不需要翻译 */
{
return ;
}

current
= root; /* 每次都要从根结点开始搜 */
for (i = 0 ; i < len; i ++ )
{
index
= fatM[i] - FIRST_CHAR; /* 获取下标 */
if (NULL != current -> child[index]) /* 当前结点存在字典树中 */
{
current
= current -> child[index]; /* 修改当前位置 */
}
else
{
puts(
" eh " );
return ;
}
}

/* 判断并输出(匹配不一定存在字典树中) */
if (current -> exist == EXIST) /* 此fatMouse单词存在字典树中 */
{
puts(current
-> trans);
}
else
{
puts(
" eh " );
}
}

/* 释放内存 */
void release(Node root)
{
int i;

for (i = 0 ; i < ALPH_LEN; i ++ )
{
if (NULL != root -> child[i])
{
release( root
-> child[i] ); /* 释放孩子结点 */
}
}

free( root );
/* 释放 */
root
= NULL; /* 指针置空 */
}

int main()
{
char trans[CHAR_LEN], fatM[CHAR_LEN];
char tmp[MAX]; /* 接收一行 */
int i, len;

if ((root = (Node) calloc ( 1 , sizeof (node))) == NULL) {
printf(
" 空间分配失败!\n " );
exit(
- 1 );
}
while (gets(tmp), len = strlen(tmp))
{
for (i = 0 ; i < len; i ++ )
{
if (tmp[i] == ' ' )
{
break ;
}
}
/* 寻找分隔点 */

/* 找出两个单词 */
strcpy(fatM, tmp
+ i + 1 );
tmp[i]
= ' \0 ' ;
strcpy(trans, tmp);

insert(fatM, trans);
/* 插入, 建立字典树 */
}

while (scanf( " %s " , fatM) != EOF)
{
translate( fatM );
}

return 0 ;
}
posted on 2010-11-22 10:36 PeckChen 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/xyoung/archive/2010/11/22/1883875.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值