当前位置:编程学习 > C/C++ >>

HDU 1075(What Are You Talking About-Trie的插入和查找)

What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 8571    Accepted Submission(s): 2696
 
 
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.
 
 
 
Author
Ignatius.L
 
 
Trie的插入和查找
话说也不难啊……
[cpp] 
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<functional>  
#include<cstdlib>  
#include<iostream>  
#include<cctype>  
using namespace std;  
#define MAXWordlen (10+10)  
#define MAXLen (3000+10)  
char s[MAXLen];  
char word[MAXWordlen],word1[MAXWordlen];  
struct node  
{  
    node *next[26+1];  
    char t[MAXWordlen];  
    node()  
    {  
        t[0]=0;  
        for (int i=0;i<=26;i++) next[i]=NULL;  
    }  
}root;  
void insert(char *str,char str1[])  
{  
    int len=strlen(str);  
    node *p=&root;  
    for (int i=0;i<len;i++)  
    {  
        if (p->next[str[i]-'a']==NULL) p->next[str[i]-'a']=new node();  
        p=p->next[str[i]-'a'];         
    }  
    strcpy(p->t,str1);  
}  
node* find(int l,int r)  
{  
    node *p=&root;  
    for (int i=l;i<=r;i++)  
    {  
        if (p->next[s[i]-'a']==NULL) return 0;  
        p=p->next[s[i]-'a'];       
    }  
    if (strcmp(p->t,"")) return p;     
    return 0;  
}  
int main()  
{  
    scanf("START");  
    while (scanf("%s",word)&&strcmp(word,"END"))  
    {  
        scanf("%s",word1);  
        insert(word1,word);  
    }  
    gets(word);  
    scanf("START");  
    gets(word);  
    while (gets(s))  
    {  
        if (!strcmp(s,"END")) break;   
        int len=strlen(s);  
        s[len+1]=0;  
        for (int i=0;i<len;i++)  
        {  
            if (islower(s[i]))  
            {  
                int j=i;  
                while (islower(s[j+1])) j++;  
                node *p=find(i,j);  
                if (p) printf("%s",p->t);  
                else  
                {  
                    for (int k=i;k<=j;k++) printf("%c",s[k]);  
                }   
                i=j;                   
            }  
            else printf("%c",s[i]);  
        }  
        printf("\n");  
    }  
    return 0;  
}  
 
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,