1. 程式人生 > >HDU 1075 What Are You Talking About(map或字典樹)

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

dict cout you als next times pre word selected

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

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