Peck Chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

这字典树敲了好多遍了...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  阅读(685)  评论(0编辑  收藏  举报