`

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;
}
 
 
0
8
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics