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): 5064    Accepted Submission(s): 1522

 

 

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('\t'), enter('\n') 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.

       題目大意:給你一些字符串和對應的字符串,然後給出一些話,然後根據之前的字符串來對應輸出,如果沒有對應,則輸出原來的字符串。
       非常裸的字典樹,很久沒寫過,這裏再寫一次。用map映射也可以,代碼短些,不過速度慢很多。
代碼:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
using namespace std;

struct node
{
    bool flag;      //標記-判斷是否有字符串
    char ch[25];    //該結點的字符串
    node *next[26]; //鏈表結點
};

node *root, memory[1000005];
int cnt = 0;

node* create_node()
{
    node *p = &memory[cnt++];
    p->ch[0] = 0;
    p->flag = false;
    for(int i = 0; i < 26; i++)
    {
        p->next[i] = NULL;
    }
    return p;
}

void insert_node(char *s, char *res)
{
    node *p = root;
    int i, k;
    for(i = 0; s[i]; i++)
    {
        k = s[i] - 'a';
        if(p->next[k] == NULL)
        {
            p->next[k] = create_node();
        }
        p = p->next[k];
    }
	strcpy(p->ch, res);
	p->flag = true;
}

char* search_node(char *s)
{
    node *p = root;
    int i, k;
    for(i = 0; s[i]; i++)
    {
        k = s[i] - 'a';
        if(p->next[k] == NULL) return s;
        else p = p->next[k];
    }
    if(p->flag == true) return p->ch;   //注意!要標記爲true才返回p->ch
    else return s;
}

int main()
{
    int i, num, len;
    char ch[25], sh[25], hh[3005];
    scanf("%s", ch);
    root = create_node();
    while(scanf("%s", ch))
    {
        if(strcmp(ch, "END") == 0) break;
        scanf("%s", sh);
        insert_node(sh, ch);
    }
    scanf("%s", ch);
    getchar();
    while(1)
    {
        gets(hh);
        if(strcmp(hh, "END") == 0) break;
        len = strlen(hh);
        hh[len] = ' ';      //末尾加多一個空格' ',更好判斷
        hh[++len] = 0;      //結束符爲0 或者 '\0'
        num = 0;
        for(i = 0; i < len; i++)
        {
            if(hh[i] >= 'a' && hh[i] <= 'z')
            {
                sh[num++] = hh[i];
            }
            else
            {
                sh[num] = 0;
                num = 0;
                cout << search_node(sh);
                if(i != len - 1) cout << hh[i];
            }
        }
        cout << endl;
    }

    return 0;
}
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章