zoukankan      html  css  js  c++  java
  • HDU 1075 What Are You Talking About(字典树)

    What Are You Talking About
    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
    Total Submission(s): 28484    Accepted Submission(s): 9712


    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(' '), enter(' ') 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!


    题意:将输入单词按输入规则翻译
    分析:将要被翻译的单词建立字典树,并在树的尾部记录实际代表的单词,查询时遇到标点查询一次树,存在则输出原本的单词,未查出则输出被查单词本身

    闲的蛋疼三种方法都试了一下,算是复习吧

    map法(链接):  Time 1872ms      Memory: 76180kB

    数组法:Time 374ms        Memory: 57204kB

    指针法:Time: 499ms       Memory: 113164kB

    /*********************数组法*/
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define maxn 666666
    using  namespace  std;
    int pos=1;
    int t[maxn][27];
    char ans[maxn][11];
    bool vis[maxn];
    void insert(char s1[],char s2[])
    {
             int x,rt=0;
             for(int i=0;s2[i];i++)
             {
                       x=s2[i]-'a';
                      if(!t[rt][x])
                               t[rt][x]=pos++;
                      rt=t[rt][x];
             }
             vis[rt]=1;//一定要标记
             strcpy(ans[rt],s1);
    }
    bool search(char s[])
    {
             int x,rt=0;
             for(int i=0;s[i];i++)
             {
                      x=s[i]-'a';
                      if(!t[rt][x])
                               return 0;
                      rt=t[rt][x];
             }
             if(vis[rt])//一定要验证,不然找到的可能是一个单词的片段而不是整个单词匹配
             {
                      printf("%s", ans[rt]);
                      return 1;
             }
             else return 0;
    }
    int main()
    {
             char s1[3111],s2[3111];//重复使用
             while(scanf("%s",s1)&&strcmp(s1,"END"))
             {
                      if(!strcmp(s1,"START")) continue;
                      scanf("%s",s2);
                      insert(s1,s2);
             }
             getchar();//去除空行
             while(gets(s2)&&strcmp(s2,"END"))
             {
                      if(!strcmp(s2,"START")) continue;
                      int c=0;
                      for(int i=0;s2[i];i++)
                      {
                               if(s2[i]>='a'&&s2[i]<='z')
                                        s1[c++]=s2[i];
                               else
                               {
                                        s1[c++]='';
                                        if(strlen(s1)&&search(s1));
                                        else
                                                 printf("%s", s1);
                                        c=0;
                                        printf("%c",s2[i]);//符号或空格
                               }
                      }
                      printf("
    ");
             }
             return 0;
    }
    /*********************指针法*/
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    struct node {
             char word[11];
             bool vis;//标记
             struct node *next[26];
             node()
             {
                      vis=0;
                      for(int i=0;i<26;i++)next[i]=0;
             }
    }*rt;
    void insert(char *s1,char *s2)
    {
             node *p=rt;
             node *tmp=0;
             for(int i=0;s2[i];i++)
             {
                      int x=s2[i]-'a';
                      if(p->next[x]==0)
                               tmp=new node,
                               p->next[x]=tmp;
                      p=p->next[x];
             }
             p->vis=1;
             strcpy(p->word,s1);
    }
    bool find(char *s)
    {
             node *p=rt;
             for(int i=0;s[i];i++)
             {
                      int x=s[i]-'a';
                      if(p->next[x]==0)
                               return 0;
                      p=p->next[x];
             }
             if(p->vis==1)
             {
                      printf("%s",p->word);
                      return  1;
             }
             else
                      return 0;
    }
    int main()
    {
             rt=new node;
             char s1[3003],s2[3003];
             while(~scanf("%s",s1)&&strcmp(s1,"END"))
             {
                      if(!strcmp(s1,"START")) continue;
                      scanf("%s",s2);
                      insert(s1,s2);
             }
             getchar();
             while(gets(s2)&&strcmp(s2,"END"))
             {
                      if(!strcmp(s2,"START")) continue;
                      int c=0;
                      for(int i=0;s2[i];i++)
                      {
                               if(s2[i]>='a'&&s2[i]<='z')
                                        s1[c++]=s2[i];
                               else
                               {
                                        s1[c++]='';
                                        if(strlen(s1)&&find(s1));
                                        else
                                                 printf("%s", s1);
                                        c=0;
                                        printf("%c",s2[i]);
                               }
                      }
                      printf("
    ");
             }
    }
    
  • 相关阅读:
    优化MyBatis配置文件中的配置
    Java多线程---同步与锁
    Runtime.getRuntime().exec()
    java ---线程wait/notify/sleep/yield/join
    redis配置详情
    httpcline
    线程
    Bootstrap学习(一)
    springmvc注解配置
    salesforce上上传和导出.csv格式文件
  • 原文地址:https://www.cnblogs.com/aeipyuan/p/9893120.html
Copyright © 2011-2022 走看看