HDOJ-1075 What Are You Talking About 字典树

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<cstring>
#include<cstdio>
#include<algorithm>
#include<cctype>
using namespace std;
struct f
{
    int next[30];
    int v;
    char englishi[15];
    void ff()
    {
        memset(next,-1,sizeof(next));
        v=0;
        englishi[0]=‘\0‘;
    }
};f s [1000000];
int cut=0;
void charu(char *s1,char *s2)
{
    int i,k=0,su;
    for (i=0;s2[i]!=‘\0‘;i++)
    {
        su=s2[i]-‘a‘;
        if (s[k].next[su]==-1)
        {
            cut++;
            s[k].next[su]=cut;
            s[cut].ff();
        }
        k=s[k].next[su];
    }
    s[k].v=1;
    strcpy(s[k].englishi,s1);
    return ;
}
int find(char *s1)
{
    int i,k=0,su;
    for (i=0;i<strlen(s1);i++)
    {
        su=s1[i]-‘a‘;
        if (s[k].next[su]==-1) break;
        k=s[k].next[su];
    }
    if (s[k].v&&s[k].englishi[0]!=‘\0‘&&i==strlen(s1))
    {
        printf("%s",s[k].englishi);
        return 0;
    }
    return 1;
}
int main()
{
    char str1[3005],str2[3005];
    int z,i;
    s[cut].ff();
    gets(str1);
    while (~scanf("%s",&str1))
    {
        if (strcmp("END",str1)==0) break;
        scanf("%s",&str2);
        charu(str1,str2);
    }
    getchar();
    gets(str1);
    while (1)
    {
        gets(str1);
        if (str1[0]==‘E‘&&str1[1]==‘N‘&&str1[2]==‘D‘&&str1[3]==‘\0‘) break;
        z=0;
        for (i=0;str1[i]!=‘\0‘;i++)
        {
            if (islower(str1[i]))
            {
                str2[z]=str1[i];
                z++;
            }
            else
            {
                str2[z]=‘\0‘;
                if (find(str2)) printf("%s",str2);
                printf("%c",str1[i]);
                z=0;
            }
        }
        if (z!=0)
        {
              str2[z]=‘\0‘;
                if (find(str2)) printf("%s",str2);
                z=0;
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-12 08:34:52

HDOJ-1075 What Are You Talking About 字典树的相关文章

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

hdoj 1004 Let the Balloon Rise(模拟 || 字典树)

Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84401    Accepted Submission(s): 31831 Problem Description Contest time again

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

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

hdoj 1251 字典树

代码: #include <stdio.h>#define  MAX    26 typedef struct TrieNode{     int nCount;      struct TrieNode *next[MAX];}TrieNode;TrieNode Memory[1000000];int allocp = 0; TrieNode *CreateTrieNode(){    int i;    TrieNode *p;    p = &Memory[allocp++]; 

hdu 1075 字典树

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

hdoj 1251 统计难题(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记录通过该结点的单词数目即可: 查找时找到前缀的最后一个单词的结点的count值即为所求: 代码如下: #include <cstdio> #include <cstring> #include <iostream> using namespace std; const in

hdu 1075(字典树)

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

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

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

hdoj 1247 Hat’s Words(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的字符串. 对于长度为LEN的字符串,其可能由LEN种可能的拼接可能:现在问题转化为查找能够拼接成该字符串的可能的两个字符串是否都在 输入的字符串中,使用字典树可以实现快速的字符串查找,算法复杂度为O(N*M),N为输入字符串的个数,M为字符串长度. 代码如下: #include <cstdio>