HDU 1075 What Are You Talking About

这题就是字典树的应用;虽然简单,但还是Runtime error 了好多次才过的,后来改了一点点东西,就不知不觉的A了。

What Are You Talking About

Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 

 

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
Output
In this problem, you have to output the translation of the history book.
Sample Input
 
  
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
Sample Output
 
  
hello, i'm from mars.
i like earth!
下面贴代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 26

typedef struct node //字典树结点信息
{
    struct node * next[max]; //26个字母的对应下标
    int flag; //标记是否是一个单词结尾
    char word[max]; //保存翻译后的单词
}node, * Node;
Node root; //根结点
char ss[3010]; //保存需要翻译的句子

void insert(char * str, char * sub) //建树
{
    int i, index, len;
    Node current, newnode;
   
    len = strlen(str);
    if(len == 0) //如果len = 0 直接跳出
        return ;
   
    current = root;
    for(i = 0; i < len; i ++)
    {
        index = str[i] - 'a'; //该单词所在的下标
        if(current->next[index] != NULL) //该字母与存在
        {
            current = current->next[index]; //指针移动
        }
        else //如果该单词还不存在,新增一个结点
        {
            newnode = (Node) calloc (1, sizeof(node)); //为新增结点分配空间
            current->next[index] = newnode;
            current = newnode;
        }
    }
   
    current->flag = 1; //标记为单词结尾
    strcpy(current->word, sub); //保存相应的翻译后的单词
}

void find(char * str, char * sub) //查找单词,并保存翻译后的单词
{
    int i, index, len;
    Node current;
   
    len = strlen(str);
    current = root;
   
    for(i = 0; i < len; i ++)
    {
        index = str[i] - 'a'; //该字母所在的下标
        if(current->next[index] != NULL) //如果该字母已存在
        {
            current = current->next[index];
        }
        else //如果该字母不存在,直接跳出
        {
            strcpy(sub, "");
            return ;
        }
    }
   
    if(current->flag = 1) //如果在字典树中找到了要找的单词,将该单词的译文保存在sub中返回
    {
        strcpy(sub, current->word);
    }
    else //字典树中没有该单词
    {
        strcpy(sub, "");
    }
}

int main()
{
    int i, lenth, k, a;
    char str1[max], str2[max], ch[max], ch1[max];
   
    root = (Node) calloc (1, sizeof(node)); //为根结点分配空间
    gets(str1);
   
    if(strcmp(str1, "START") == 0) //下面开始输入单词跟对应的译文
    {
        scanf("%s", str1);
        while(strcmp(str1, "END") != 0)
        {
            scanf("%s", str2);
            insert(str2, str1); //建树
            scanf("%s", str1);
        }
    }
   
    scanf("%s", str1);
    getchar();

    if(strcmp(str1, "START") == 0)
    {
        gets(ss); //输入要翻译的句子
       
        while(strcmp(ss, "END") != 0)
        {
            lenth = strlen(ss);
            a = 0;
            for(k = 0; k < lenth; k ++)
            {
                if(ss[k] >= 'a' && ss[k] <= 'z')
                {
                    ch[a ++] = ss[k];
                }           
                else
                {
                    ch[a] = '\0';
                    find(ch, ch1); //查找
                   
                    if(strcmp(ch1, "") != 0)
                    {
                        printf("%s", ch1);
                    }
                    else
                    {
                        printf("%s", ch);
                    }
                    putchar(ss[k]);
                    a = 0;
                }           
            }
           
            printf("\n");
            gets(ss);
        }
    }
   
    return 0;
}

转载于:https://www.cnblogs.com/Mr_Lai/archive/2010/11/27/1889731.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值