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): 25869    Accepted Submission(s): 8813


    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!
    Hint
    Huge input, scanf is recommended.
     
    Author
    Ignatius.L
     
    Recommend
    We have carefully selected several similar problems for you:  1251 1671 1026 1016 1015 
     
    题目大意:将给出的火星字符串翻译成英文字符串,每个单词有对应的英文单词,如果不存在对应的就输出原有的即可。
    解题思路:对火星字符串进行建树,在最后一个字母上存下对应的英文字符串。这里要注意一个标记,不然会超内存。
    如图。画了两个具体说明一下,其余的以此类推。
      1 #include <iostream>
      2 #include <cstring>
      3 #include <string>
      4 #include <map>
      5 #include <algorithm>
      6 #include <cctype>
      7 using namespace std;
      8 typedef struct nn
      9 {
     10     int f;
     11     char *c;
     12     struct nn *nxt[26];
     13 }node;
     14 
     15 node *builde()
     16 {
     17     node*p = (node *)malloc(sizeof(node));
     18     p->f = 0;
     19     for (int i = 0; i < 26; i++)
     20     {
     21         p->nxt[i] = NULL;
     22     }
     23     return p;
     24 }
     25 
     26 node *root,*p;
     27 
     28 void insert(char*t, char *s)//s是原语言,t是英文
     29 {
     30     p = root;
     31     int i;
     32     for (i = 0; s[i] != ''; i++)
     33     {
     34         if (p->nxt[s[i] - 'a'] == NULL)
     35         {
     36             p->nxt[s[i] - 'a'] = builde();
     37         }
     38         p = p->nxt[s[i] - 'a'];
     39     }
     40     p->f = 1;
     41     int l = strlen(t);
     42     p->c = (char *)malloc((l+1)*sizeof(char)); // 申请动态空间
     43     strcpy(p->c,t);
     44 }
     45 
     46 void search(char *s)
     47 {
     48     p = root;
     49     if (strlen(s) == 0) return;
     50     for (int i = 0; s[i] != '';i++)
     51     {
     52         if (p->nxt[s[i] - 'a'] == NULL)
     53         {
     54             cout << s;
     55             return;
     56         }
     57         p = p->nxt[s[i] - 'a'];
     58     }
     59     if (p->f)
     60         cout << p->c;
     61     else
     62         cout << s;
     63 }
     64 
     65 int main()
     66 {
     67     char t[15], s[3005], s1[3005];
     68     root = builde();
     69     p=builde();
     70     while (cin >> s)
     71     {
     72         if (strcmp(s,"START")==0) continue;
     73         if (strcmp(s, "END") == 0)
     74             break;
     75         cin >> s1;
     76         insert(s, s1);
     77     }
     78     scanf("%s", &t);
     79     getchar();
     80     while (gets(s))
     81     {
     82         if (strcmp(s, "END") == 0) break;
     83         int i;
     84         int k=0;
     85         for (i = 0; s[i] != ''; i++)
     86         {
     87             if (s[i]<'a' || s[i]>'z')
     88             {
     89                 s1[k] = '';
     90                 search(s1);
     91                 cout << s[i];
     92                 k = 0;
     93             }
     94             else
     95                 s1[k++] = s[i];
     96         }
     97         cout << endl;
     98     }
     99     return 0;
    100 }
    用map更方便
     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <map>
     5 #include <algorithm>
     6 #include <cctype>
     7 using namespace std;
     8 int main()
     9 {
    10     map<string,string>m;
    11     string a;
    12     string s;
    13     cin >> s;
    14     while (cin >> a && a != "END")
    15     {
    16         cin >> s;
    17         m[s] = a;
    18     }
    19     cin >> s;
    20     getchar();
    21     char c[3005];
    22     while (1)
    23     {
    24         gets(c);
    25         int i, l;
    26         if (strcmp(c, "END") == 0) break;
    27         l = strlen(c);
    28         s = "";
    29         for (i = 0; i < l; i++)
    30         {
    31             if (c[i]<'a' || c[i]>'z')
    32             {
    33                 if (m[s] != "") cout << m[s];
    34                 else cout << s;
    35                 s = "";
    36                 cout << c[i];
    37             }
    38             else s = s + c[i];
    39         }
    40         cout << endl;
    41     }
    42     return 0;
    43 }
     
  • 相关阅读:
    Intent
    What should we do next in general after collecting relevant data
    NOTE FOR Secure Friend Discovery in Mobile Social Networks
    missing pcap.h
    after building Android Source code
    plot point(one column)
    When talking to someone else, don't infer that is has been talked with others at first. It may bring repulsion to the person who is talking with you.
    进程基本知识
    Python input和raw_input的区别
    强制 code review:reviewboard+svn 的方案
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8452155.html
Copyright © 2011-2022 走看看