现在的位置: 首页 > 综合 > 正文

HDOJ–1075–What Are You Talking About【字典树】

2018年04月24日 ⁄ 综合 ⁄ 共 2842字 ⁄ 字号 评论关闭
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.
 


Author
Ignatius.L
 

字典树的插入和查找,我的代码有些单词会被前缀影响,有些不会,不知道改哪,在网上找了一个代码,发现自己没有将字典树的后缀初始化为空集,所以后来再判断空集时出现错误,感觉略坑

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string.h>
#include<ctype.h>
#include<stack>
#include<queue>
#include<set>
#include<algorithm>
#include  <limits.h>
#define PI acos(-1)
#define MAX 26
using namespace std;
char str[40000];

typedef struct Trie
{
    Trie *next[MAX];
    char s[20];
    Trie()
    {
        s[0]=0;
        for (int i=0;i<=26;i++) next[i]=NULL;
    }
};
Trie root;
void addTrie(char *str2,char str1[])
{

    int len = strlen(str2);
    Trie *p = &root, *q;
    for(int i=0; i<len; ++i)
    {
        int id = str2[i]-'a';
        if(p->next[id] == NULL)
        {
            q = new Trie;
            //for(int j=0; j<MAX; ++j)
              //  q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else p=p->next[id];
    }
    strcpy(p->s,str1);
}
Trie* findTrie(int l,int r)
{
    Trie *p = &root;
    for(int i=l; i<=r; ++i)
    {
        int id = str[i]-'a';
        if(p->next[id] == NULL)   //若为空集,表示不存以此为前缀的串
            return 0;
        p = p->next[id];
    }
    if (strcmp(p->s,"")) return p;
    return 0;
}
int main()
{
    char str1[20],str2[20],c;
    scanf("%s",str1);
    while(scanf("%s",str1),strcmp(str1,"END"))
    {
        scanf("%s",str2);
        addTrie(str2,str1);
    }
    scanf("%s",str1);
    c=getchar();
    while(gets(str),strcmp(str,"END"))
    {
        int len=strlen(str);
        str[len+1]=0;
        for (int i=0;i<len;i++)
        {
            if(isalpha(str[i]))
            {
                int j=i;
                while (isalpha(str[j+1])) j++;
                Trie *p=findTrie(i,j);
                if (p) printf("%s",p->s);
                else
                {
                    for (int k=i;k<=j;k++) printf("%c",str[k]);
                }
                i=j;
            }
            else printf("%c",str[i]);
        }
        printf("\n");
    }
    return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.