• hdu 1075 What Are You Talking About


    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
    Total Submission(s): 28719    Accepted Submission(s): 9792


    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:  1800 1671 1298 1026 1016 
     
    每行给出两个单词,记录第一单词,把第二个插入字典树,同时结尾指向第一个单词的下标,构建字典。
    对每行进行遍历,查询得到下标如果不是0,就进行转换,否则输出原串。
    代码:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <set>
    #define inf 0x3f3f3f3f
    #define MAX 100000
    using namespace std;
    int trie[MAX * 10][26],to[MAX * 10],pos,num;
    char eng[MAX][11];
    void Insert(char *s,int no) {
        int i = 0,c = 0;
        while(s[i]) {
            int d = s[i] - 'a';
            if(!trie[c][d]) trie[c][d] = ++ pos;
            c = trie[c][d];
            i ++;
        }
        to[c] = no;
    }
    int Query(char *s) {
        int i = 0,c = 0;
        while(s[i]) {
            int d = s[i] - 'a';
            if(!trie[c][d]) trie[c][d] = ++ pos;
            c = trie[c][d];
            i ++;
        }
        return to[c];
    }
    int main() {
        char str[4000],str1[100];
        scanf("%s",str);
        while(scanf("%s",str) && strcmp(str,"END")) {
            strcpy(eng[++ num],str);
            scanf("%s",str);
            Insert(str,num);
        }
        scanf("%s
    ",str);///!!!!
        while(gets(str) && strcmp(str,"END")) {
            string ans = "";
            int c = 0;
            for(int i = 0;str[i];i ++) {
                if(isalpha(str[i])) {
                    str1[c ++] = str[i];
                }
                else {
                    if(c) {
                        str1[c] = 0;
                        int d = Query(str1);
                        if(d)ans += eng[d];
                        else ans += str1;
                        c = 0;
                    }
                    ans += str[i];
                }
            }
            printf("%s
    ",ans.c_str());
        }
    }
  • 相关阅读:
    Python List+Tuple+Dict+Set小结
    小命要紧~风热风寒感冒
    IDEA忽略编译报错
    IDEA新建一个Spring Boot项目
    ip2region.jar实现ip转地址
    IDEA控制台中文乱码问题
    IDEA报错Plugin "XXX" was not loaded: required plugin "Java EE: EJB, JPA, Servlets" is disabled.
    IDEA Error:java: Compilation failed: internal java compiler error
    Failed to start component [StandardEngine[Catalina].StandardHost[localhost]
    浏览网页隐藏服务器IP
  • 原文地址:https://www.cnblogs.com/8023spz/p/9633334.html
Copyright © 2020-2023  润新知