hdu1075 字典树

来源:互联网 发布:在淘宝买steam游戏 编辑:程序博客网 时间:2024/05/22 14:33

这是第一次接触使用字典树,想想都有些小激动,特别还是把freopen一起交的那种。


字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。(我从百度上抓的。)


字典树模板:

typedef struct tire_node{    int count;    struct tire_node *next[26];    int exist ;    char trans[11];}TireNode,*Tire;//创建树TireNode* createTireNode(){    TireNode* node=(TireNode *)malloc(sizeof(TireNode));    node->count=0;    node->exist=0;    memset(node->next,0,sizeof(node->next));    return node;}//字典插入void Tire_insert(Tire root,char *word,char *trans){    TireNode *node=root;    char *p=word;    int id=0;    while(*p){        id=*p-'a';        if(node->next[id]==0){            node->next[id]=createTireNode();        }        node=node->next[id];        ++p;        node->count+=1;    }    node->exist=1;    strcpy(node->trans,trans);}//字典查找char *Tire_search(Tire root,char *word){    TireNode *node=root;    int id=0;    char *p=word;    while(*p){        id=*p-'a';        node=node->next[id];        ++p;        if(node==NULL)            return 0;    }    if(node->exist){        return node->trans;    }    else{        return NULL;    }}

咋们再来看看题目;

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 18206    Accepted Submission(s): 5913


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

STARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!END

 


Sample Output

hello, i'm from mars.i like earth!

题意大概就是先给你一堆字典,然后给你一堆每行不超过3000字符的字符串让你按照字典翻译。

做法:先把字典存到字典树里面,然后扫给出的字符串,遇到非字母的就在字典树里面找在扫描过程中遇到的单词,如果有就输出翻译,没有输出原话及非字母字符。

值得提醒的是要注意对末尾的处理,如果扫到了末尾,看看还有单词要翻译没有,最多再查找一次字典树。

AC代码

#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct tire_node{tire_node *next[26];char trans[11];int exist;}TireNode;TireNode* createTireNode(){TireNode* node = (TireNode *)malloc(sizeof(TireNode));node->exist = 0;memset(node->next, 0, sizeof(node->next));return node;}void Tire_insert(TireNode * root, char str[], char trans[]){TireNode *node = root;char *p = str;int id = 0;while (*p){id = *p - 'a';if (node->next[id] == 0){node->next[id] = createTireNode();}node = node->next[id];++p;}node->exist = 1;strcpy(node->trans, trans);}char * Tire_search(TireNode *root, char str[]){TireNode *node = root;char *p = str;int id = 0;while (*p){id = *p - 'a';node = node->next[id];++p;if (node == NULL){return NULL;}}if (node->exist == 1){return node->trans;}else{return NULL;}}void solve(TireNode * root, char line[]){char ms[15];char *p = line;char *q = ms;memset(ms, 0, sizeof(ms));for (int i = 0; i<3005; i++){if ('a' <= *p&&*p <= 'z'){*q = *p;q++; p++;}else{if(ms[0]!=0){                *q=0;                char *trans=Tire_search(root,ms);                if(trans!=NULL){                    printf("%s",trans);                }                else                    printf("%s",ms);                }if(*p!=0){                printf("%c",*p);}else{                break;}memset(ms,0,sizeof(ms));q=ms;p++;}}}int main(){TireNode *root = createTireNode();char line[3005];char ms[15], trans[15];while (scanf("%s",line)!=EOF){while (scanf("%s", trans)){if (strcmp(trans, "END") == 0)break;            scanf("%s",ms);Tire_insert(root, ms, trans);}getchar();gets(line);while (gets(line)!=NULL){if (strcmp(line, "END") == 0)break;solve(root, line);printf("\n");}}return 0;}




0 0