HDU 1075 What Are You Talking About(Trie的应用)

What Are You Talking About

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

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.

题目链接:HDU 1075

结构体Trie中用flag=1表示当前点是否是某个单词的结尾处(防止出现单词a为单词b的前缀但是却被b覆盖的情况,比如diff对应bbb,但是此时diff的前缀dif显然是不存在的),若为结尾,则flag赋值为1,然后把对应的翻译单词赋值给这个节点的s[]。

查询的是否看查完之后的flag是否为1,如果为1则存在该单词,否则flag为0或根本查不到,则返回NULL

一开始智障了把每个点flag都设为1狂WA……

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=26;
const int M=100;
struct Trie
{
    Trie *nxt[N];
    int flag;
    char s[M];
    Trie()
    {
        CLR(nxt,0);
        CLR(s,0);
        flag=0;
    }
};
Trie *L=new Trie();
void update(char s[],char earth[])
{
    int len=strlen(s);
    int indx;
    Trie *cur=L;
    for (int i=0; i<len; ++i)
    {
        indx=s[i]-‘a‘;
        if(cur->nxt[indx])
            cur=cur->nxt[indx];
        else
        {
            Trie *one=new Trie();
            cur->nxt[indx]=one;
            cur=one;
        }
    }
    strcpy(cur->s,earth);
    cur->flag=1;
}
char* Find(char s[])
{
    int len=strlen(s);
    int indx;
    Trie *cur=L;
    for (int i=0; i<len; ++i)
    {
        indx=s[i]-‘a‘;
        if(cur->nxt[indx]==NULL)
            return NULL;
        cur=cur->nxt[indx];
    }
    return cur->flag?cur->s:NULL;
}
char from[M],to[M];
char tot[1000010];
char temp[M];
int main(void)
{
    int i;
    scanf("%s",from);
    getchar();
    while (~scanf("%s",to)&&strcmp(to,"END"))
    {
        scanf("%s",from);
        update(from,to);
    }
    getchar();

    gets(from);
    while (gets(tot)&&strcmp(tot,"END"))
    {
        int len=strlen(tot);
        int k=0;
        CLR(temp,0);
        for (i=0; i<len; ++i)
        {
            if(islower(tot[i]))
                temp[k++]=tot[i];
            else
            {
                char *one=Find(temp);
                if(k)
                    printf("%s",one==NULL?temp:one);
                putchar(tot[i]);
                CLR(temp,0);
                k=0;
            }
        }
        if(k)
        {
            char *one=Find(temp);
            printf("%s",one==NULL?temp:one);
        }
        putchar(‘\n‘);
    }
    return 0;
}
时间: 2024-10-12 01:35:27

HDU 1075 What Are You Talking About(Trie的应用)的相关文章

HDU 1075 What Are You Talking About Trie题解

翻译火星语,不过火星语也是使用英文单词的,就是把一个单词对应到另外一个单词. 可以使用map, 使用二分,方法很多. 不过最快的应该都是Trie解法了. 把火星语挂在Trie树中,然后在叶子节点增加一个string容器,装英语单词. 查找的时候,找到了出现在Trie中的火星语,就返回string就可以了. #include <stdio.h> #include <string> #include <string.h> using namespace std; const

HDU 1075 What Are You Talking About (map解法+Trie解法)

HDU 1075 What Are You Talking About (map解法+Trie解法) ACM 题目地址: HDU 1075 What Are You Talking About 题意: 给出一个"翻译-原文"的对应表,然后给出句子,要把句子中的原文都翻译出来. 分析: 可以用map赤裸裸地做,但是比较花费时间,虽然这题时间给了5s,map解法是能过的. 不过Trie解法500+ms,果然Trie字典树才是正解啊. Trie入门题. 另外发现ios_base::sync_

hdu 1075 字典树

// hdu 1075 字典树 // // 题目大意: // // 给你一个字典,即有两个字符串,一个是英文,一个是火星文,然后 // 输入一段火星文,要你翻译成英文. // // 解题思路: // // 字典树,查字典嘛,有就输出查到的,没有原样输出.将火星文插入到 // 字典树中,然后在字典输中查找.找到了,输出对应的英文,否则,原样输 // 出. // // 感悟: // // 题目确实很简单,但是,没告诉数据范围啊,导致我一直RE,原来单词 // 可能对应很长的英文啊,找人家ac的开数组

HDU 11488 Hyper Prefix Sets (字符串-Trie树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

HDU 1075 map or 字典树

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4069 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 1075 What Are You Talking About (Trie树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 map可以过...我上的字典树,小bug有点尴尬,题目没有明确给出数据范围也是无奈. 贡献了几次RE 一次WA.尴尬.discuss里面有个说注意前缀的到是给了点tip.总体来说不错 代码: 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <

HDU 1075 What Are You Talking About (Trie)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 16042    Accepted Submission(s): 5198 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdu 1075 What Are You Talking About 字典树 trie

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

HDU 1075 What Are You Talking About(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:根据词典翻译语句. 思路:裸的字典树.每个节点存以该节点为结尾的对应的单词. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string> #incl