您的位置:首页 > 其它

hdu 1075 What Are You Talking About 字典树 trie

2015-01-30 21:24 549 查看
What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)

Total Submission(s): 14703 Accepted Submission(s): 4724

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!

Hint

Huge input, scanf is recommended.



题意:之前输入的是映射,后面输入的是文章。

我节点里存的是字符串在数组中的标号。

用map可以比较轻松的过。但是看到hint,还是想用c语言打一遍。

比较裸的最小生成树题目。开始打完RE了,检查了一个晚上字典树代码。后来发现是存映射字符串的arr开小了。出题人说的3000个单词指的是后面的文章中的句子。我理解成查找的映射只有3000个,后来开到50000也RE,开到500000,才通过。

但是后来有wa了,发现是之前打的末尾不是符号会出错。比如说下面这组数据

START

from fiwo

END

START

difh, i'm fiwo riwosf fiwo

END

一波三折,庆幸好过了。


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

#define MAX 30
typedef struct Trie   
{   
    Trie *next[MAX];   
    int v;   //根据需要变化
};   
 
Trie root;
char str[35000];
char len[35000];
void createTrie(char *str,int wei)
{
    int len = strlen(str);
    Trie *p = &root, *q;
    for(int i=0; i<len; ++i)
    {
        int id = str[i]-'a';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(Trie));
            q->v = -1;    //把不是字符串最后一个的v 都初始化为-1
            for(int j=0; j<MAX; ++j)
                q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            p = p->next[id];
        }
    }
    p->v = wei;   //若为结尾,则将v改成wei表示这串字符串对应的字符串在arr中的下标
}

int findTrie(char *str)
{
    int len = strlen(str);
    Trie *p = &root;
    for(int i=0; i<len; ++i)
    {
        int id = str[i]-'a';
        p = p->next[id];
        if(p == NULL)   //已经找不到第i+1个字母了,所以不存在这个字符串
            return -1;
        if(p->v != -1&&i==len-1)   
            return p->v;
    }
    return -1;   //此串是字符集中某串的前缀
}
char arr[500000][15];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("output.txt","w",stdout);
#endif
    int n;
    scanf("%s",str);
    for(int i=0;;i++)
    {
        scanf("%s",arr[i]);
        if(strcmp(arr[i],"END")==0)
        {
            scanf("%s",arr[i]);
            break;
        }
        scanf("%s",str);
        createTrie(str,i);
    }
    getchar();
    char c;
    int p=0;
    while(1)
    { 
        gets(len);
        if(strcmp(len,"END")==0)
            break;
        int nn=strlen(len);
        len[nn]=10;//因为是gets输入没有回车,所以添加一个回车
        len[nn+1]=0;
        for(int i=0;len[i];i++)
        {
            c=len[i];
            if((c<='z'&&c>='a'))
                str[p++]=c;
            else
            {
                str[p]=0;
                int tem=findTrie(str);
                if(tem!=-1)
                    printf("%s%c",arr[tem],c);
                else 
                    printf("%s%c",str,c);
                p=0;
            }
        }
    }
    return 0;
}

/*
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!

START
dog aa
END
START
a aa
END
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: