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

    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.

    题意:这题就是翻译文章,所以把所有译文存到数组中,对每个单词插入进字典树,并在结点上增加译文在数组中位置的信息,输出时只需查找单词,若存在则输出译文,否组输出原文即可。

    依旧是简单的字典树模版题。

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cstdio>
    using namespace std;
    char s[30] , sl[505000][30] , sg[30];
    struct TnT {
        TnT *next[27];
        int v;
        TnT():v(0) {
            memset(next , 0 , sizeof(next));
        }
    };
    void build(TnT *root , char s[] , int num) {
        int len = strlen(s);
        TnT *p = root;
        for(int i = 0 ; i < len ; i++) {
            int id = s[i] - 'a';
            if(p->next[id] == NULL) {
                p->next[id] = new TnT;
            }
            p = p->next[id];
        }
        p->v = num;
    }
    int find(TnT *root , char s[]) {
        int temp = 0;
        int len = strlen(s);
        TnT *p = root;
        for(int i = 0 ; i < len ; i++) {
            int id = s[i] - 'a';
            if(p->next[id] == NULL) {
                return 0;
            }
            p = p->next[id];
        }
        return p->v;
    }
    void de(TnT *root) {
        if(root == NULL) {
            return;
        }
        else {
            for(int i = 0 ; i < 26 ; i++) {
                de(root->next[i]);
            }
        }
        delete root;
    }
    int main()
    {
        TnT *root = new TnT;
        int count = 1;
        while(gets(s)) {
            if(strcmp(s , "START") == 0) {
                continue;
            }
            if(strcmp(s , "END") == 0) {
                break;
            }
            int len = strlen(s);
            int flag = 0;
            int temp = 0;
            for(int i = 0 ; i < len ; i++) {
                if(flag == 0 && s[i] >= 'a' && s[i] <= 'z') {
                    sl[count][temp++] = s[i];
                }
                if(flag == 1 && s[i] >= 'a' && s[i] <= 'z') {
                    sg[temp++] = s[i];
                }
                if(s[i] == ' ') {
                    flag = 1;
                    temp = 0;
                }
            }
            build(root , sg , count);
            count++;
            memset(sg , 0 , sizeof(sg));
        }
        while(gets(s)) {
            if(strcmp(s , "START") == 0) {
                continue;
            }
            if(strcmp(s , "END") == 0) {
                break;
            }
            int len = strlen(s);
            int temp = 0;
            for(int i = 0 ; i < len ; i++) {
                if(s[i] >= 'a' && s[i] <= 'z') {
                    sg[temp++] = s[i];
                }
                else {
                    temp = 0;
                    int gg = find(root , sg);
                    if(gg) {
                        printf("%s" , sl[gg]);
                    }
                    else {
                        printf("%s" , sg);
                    }
                    memset(sg , 0 , sizeof(sg));
                    printf("%c" , s[i]);
                }
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    String类的常用方法(P小写)
    二维数组:判断是否有目标数
    java实现输入年份判断在哪一天(正则表达式待改进)
    Java实现八进制正整数转化为十进制数
    时钟和定时器
    电路的频率响应---带宽的定义
    stm32两轮平衡车资料
    二阶常系数齐次线性微分方程的解法
    同步积分
    陀螺仪信号解调
  • 原文地址:https://www.cnblogs.com/TnT2333333/p/6075560.html
Copyright © 2011-2022 走看看