zoukankan      html  css  js  c++  java
  • HDU- 1075 What Are You Talking About (Trie or map)

    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和Trie都可以做。
    • 不过Trie的时间还是比map快了将近一倍。
    • 提交结果如下:
    • 这里写图片描述

    map代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define MAXN 30000+10
     4 map<string, string> mp;
     5 map<string, string>::iterator iter;
     6 
     7 bool word(char ch){
     8     if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
     9         return true;
    10     return false;
    11 }
    12 
    13 int main()
    14 {
    15     char e[20];
    16     char s1[20], s2[20], s3[20], ch;
    17     scanf("%s", e);//START
    18     while(scanf("%s", s1), strcmp(s1, "END")){
    19         scanf("%s", s2);
    20         mp[s2] = s1;
    21     }
    22     scanf("%s", e);//START
    23     int len = 0;
    24     getchar();
    25     while(ch = getchar()){
    26         if (word(ch)){
    27             s3[len++] = ch;
    28         }
    29         else{
    30             s3[len] = '';
    31             if (!strcmp(s3, "END")) break;
    32             if (mp.count(s3)) cout<<mp[s3];
    33             else cout<<s3;
    34             len = 0;
    35             printf("%c", ch);
    36         }
    37     }
    38 
    39     return 0;
    40 }

    Trie代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<set>
     8 
     9 using namespace std;
    10 
    11 bool word(char ch){
    12     if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
    13         return true;
    14     return false;
    15 }
    16 
    17 typedef struct TrieNode{
    18     char s[20];
    19     bool flag;
    20     TrieNode *next[26];
    21     TrieNode(){
    22         flag = false;
    23         memset(next, 0, sizeof(next));
    24         memset(s, 0, sizeof(s));
    25     }
    26 }TrieNode, *Trie;
    27 
    28 TrieNode *InitTrieNode(){
    29     TrieNode *p = new TrieNode;
    30     return p;
    31 }
    32 
    33 void InsertNode(Trie root, char *word, char *ss){
    34     Trie p = root;
    35     char *ch = word;
    36     int id;
    37     while(*ch){
    38         id = *ch - 'a';
    39         if (p->next[id] == NULL)
    40             p->next[id] = InitTrieNode();
    41         p = p->next[id];
    42         ++ch;
    43     }
    44     p->flag = true;
    45     strcpy(p->s, ss);
    46 }
    47 
    48 bool TrieSearch(Trie root, char *word, char *e){
    49     Trie p = root;
    50     char *ch = word;
    51     int id;
    52     bool ans = false;
    53     while(*ch){
    54         id = *ch - 'a';
    55         if (p->next[id] == NULL)
    56             return false;
    57         p = p->next[id];
    58         ++ch;
    59     }
    60     strcpy(e, p->s);
    61     if (p->flag) return true;
    62     else return false;
    63 }
    64 
    65 int main()
    66 {
    67     Trie root = InitTrieNode();
    68     char s1[20], s2[20], s3[20], ch;
    69     scanf("%s", s1);//START
    70     while(scanf("%s", s1), strcmp(s1, "END")){
    71         scanf("%s", s2);
    72         InsertNode(root, s2, s1);
    73     }
    74     scanf("%s", s1);//START
    75     getchar();
    76     int len = 0, ans;
    77     while(ch = getchar()){
    78         if (word(ch)){
    79             s3[len++] = ch;
    80         }
    81         else{
    82             s3[len] = '';
    83             if (!strcmp(s3, "END")) break;
    84             if (TrieSearch(root, s3, s2)) printf("%s", s2);
    85             else printf("%s", s3);
    86             len = 0;
    87             printf("%c", ch);
    88         }
    89     }
    90 
    91 
    92     return 0;
    93 }
  • 相关阅读:
    【Vue】 修饰符sync
    【VUE】vue路由跳转的方式
    【Element】elementui的Cascader 级联选择器,在懒加载的时候数据无法回显的解决方案
    【ES6】利用ES6 Set 将数组去重
    【.NETCORE】Refit 框架
    【.NETCORE】ASP.NET Core SignalR
    【Visual Studio Code】驼峰翻译助手
    VueX(Vue状态管理模式)
    hdmi 随笔
    ad 差分布线 等长布线
  • 原文地址:https://www.cnblogs.com/robin1998/p/6359122.html
Copyright © 2011-2022 走看看