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 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

Recommend

We have carefully selected several similar problems for you:  1800 1298 1026 1016 1072

题意 第一个start和end之间的字符串中,表示字典,后者可以翻译成前者。

第二个start与end之间,每行一句话,进行翻译,如果该单词在字典中存在,就翻译再输出,否则直接输出,符号等也直接输出

使用字典树解决该问题的时间效率更高

map代码(1100ms)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
map<string,string>mp;
char str1[15];
char str2[15];
char str[3100];
char res[3100];
int len;
int main()
{
   scanf("%s",str1);
   while(scanf("%s",str1)!=EOF)
   {
      if(!strcmp(str1,"END"))
        break;
      scanf("%s",str2);
      mp[str2]=str1;
    //   cout<<mp[str2]<<endl;
   }
   getchar();
   gets(str);
   while(gets(str))
   {
       if(!strcmp(str,"END"))
        break;
        int l=0;
        int r=0;
        for(int i=0;i<strlen(str);i++)
        {
            if(str[i]>=‘a‘&&str[i]<=‘z‘)
            res[l++]=str[i];
            else
            {
              res[l]=0;
             if(mp.find(res)!=mp.end())  //如果mp中不存在该元素,mp.find()==mp.end()
               cout<<mp[res];
              else
              printf("%s",res);
              printf("%c",str[i]);
              l=0;
            }
        }
        printf("\n");
   }

 return 0;
}

字典树代码(700ms)

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN=26;
typedef struct Trie{
   char* v;
   Trie *next[MAXN];
}Trie;
Trie *root;
void createTrie(char *str,char *str2)
{
    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));
         for(int j=0;j<MAXN;j++)
            q->next[j]=NULL;
            q->v=NULL;
         p->next[id]=q;
         p=p->next[id];
     }
     else
     {
         p=p->next[id];
     }
    }
    p->v=new char[11];  //给指针v分配一个长度为10的字符串
  strcpy(p->v,str2);
}
char* 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)
            return 0;
    }
    return p->v;
}

int  deal(Trie *T)
{
    int i;
    if(T==NULL)
        return 0;
    for(int i=0;i<MAXN;i++)
    {
        if(T->next[i]!=NULL)
            deal(T->next[i]);
    }
    free(T);
    return 0;
}
char str1[15];
char str2[15];
char str[3100];
char res[3100];
int len;
char *q;
int main()
{
        root=(Trie*)malloc(sizeof(Trie));
    for(int i=0;i<MAXN;i++)
      root->next[i]=NULL;
      root->v=NULL;
      scanf("%s",str1);
   while(scanf("%s",str1)!=EOF)
   {
      if(!strcmp(str1,"END"))
        break;
      scanf("%s",str2);
      createTrie(str2,str1);
    //   cout<<mp[str2]<<endl;
   }
   getchar();
   gets(str);
   while(gets(str))
   {
       if(!strcmp(str,"END"))
        break;
        int l=0;
        int r=0;
        for(int i=0;i<strlen(str);i++)
        {
            if(str[i]>=‘a‘&&str[i]<=‘z‘)
            res[l++]=str[i];
            else
            {
              res[l]=0;
              q=findTrie(res);
                 if(q)
               {
                 printf("%s",q);
               }
              else
              printf("%s",res);
              printf("%c",str[i]);
              l=0;
            }
        }
        printf("\n");
   }
    deal(root);
    return 0;
}
时间: 2024-10-17 01:35:02

HDU 1075 What Are You Talking About(map或字典树)的相关文章

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(给你字典,让你翻译句子,字典中查不到的单词不用翻译)

1.这是一道字典树的题,但用map也可以做 2.代码: #include<cstdio> #include<cstring> #include<iostream> #include<map> using namespace std; map<string,string> mp; char s[30100],ss[15]; int main() { scanf("%s",s); while(1) { scanf("%s

POJ_2503_Babelfish(map or 字典树)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 34816   Accepted: 14908 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

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): 16042    Accepted Submission(s): 5198 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

51nod 1095 Anigram单词【hash/map/排序/字典树】

1095 Anigram单词 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的Anigram,例如单词army和mary互为Anigram.现在给定一个字典,输入Q个单词,从给出的字典中找出这些单词的Anigram. Input 第1行:1个数N,表示字典中单词的数量.(1 <= N <= 10000) 第2 - N + 1行,字典中的单词,单词长度 <= 10

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 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题解

翻译火星语,不过火星语也是使用英文单词的,就是把一个单词对应到另外一个单词. 可以使用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(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