HDu 1075 What Are You Talking About【ディクショナリツリー】

6831 ワード

What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 18471    Accepted Submission(s): 6036
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('') 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.


一道挺好的学习字典树的题、这里我在做题过程中遇到了这样的几个问题,也许大家会跟我犯一样的毛病:

1、PE、在有gets和scanf同时使用的时候,由于某些原因自动输出一个换行符、getchar可解决之。

2、TLE、MLE、尝试换成C++提交、尝试开大数组,这里测试数据是很令人心酸的。(也别过分离谱的开大数组)

3、WA、这里我提供一组数据,也许你就WA在这里:

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!fn f fnn
END

这里先详解处理输入部分:

    while(~scanf("%s",b))
    {
        if(strcmp(b,"START")==0)continue;
        if(strcmp(b,"END")==0)break;
        scanf("%s",a);
        creat(a);// a  b
    }
    getchar();
    while(gets(c))
    {
        if(strcmp(c,"START")==0)continue;
        if(strcmp(c,"END")==0)break;
        memset(str,'\0',sizeof(str));
        for(int i=0;i
それから先ほど述べた第3の点をどのように処理するかを強調して、辞書の木の作用は接頭辞を探して、もし接頭辞だけを見つけて出力するならば、きっとwaができて、だから私達はここで初期化する時、1つのタグの変数をプラスして、creatの時の最も後の1つのアルファベットでタグに値を割り当てて、findの時、もし調べた地方のタグに値が割り当てられているならば、では出力します.そうしないと見つかりません.
また,タグに値を付与するとともに,ノードstrcpyにマッピングされた単語,
ここに処理コードを貼り付けます.
void creat(char *str)
{
    int len=strlen(str);
    tree *p=&root,*q;
    for(int i=0;inext[id]==NULL)
        {
            q=(tree *)malloc(sizeof(root));
            for(int j=0;j<26;j++)
            {
                q->next[j]=NULL;
            }
            p->next[id]=q;
        }
        p=p->next[id];
        if(i==len-1)//  creat        ;
        {
            p->flag=1;//    1
            strcpy(p->la,b);//strcpy  b
        }
    }
}
int  find(char *str)
{
    int len=strlen(str);
    tree *p=&root;
    for(int i=0;inext[id];
        if(p==NULL)
        return 0;
    }
    if(p->flag==1)//         。
    printf("%s",p->la);
    else
    return 0;
    return 1;
}
そして完全なACコード:
#include
#include
#include
#include
using namespace std;
#define maxn 26//       、
typedef struct tree
{
    int flag;
    tree *next[maxn];
    char  la[30];
}tree;
tree root;
char b[10000000];
char c[10000000];
char str[1000];
void creat(char *str)
{
    int len=strlen(str);
    tree *p=&root,*q;
    for(int i=0;inext[id]==NULL)
        {
            q=(tree *)malloc(sizeof(root));
            for(int j=0;j<26;j++)
            {
                q->next[j]=NULL;
            }
            p->next[id]=q;
        }
        p=p->next[id];
        if(i==len-1)
        {
            p->flag=1;
            strcpy(p->la,b);
        }
    }
}
int  find(char *str)
{
    int len=strlen(str);
    tree *p=&root;
    for(int i=0;inext[id];
        if(p==NULL)
        return 0;
    }
    if(p->flag==1)
    printf("%s",p->la);
    else
    return 0;
    return 1;
}
int main()
{
    char a[20];
    for(int i=0;i<26;i++)
    {
        root.flag=0;
        root.next[i]=NULL;
    }
    while(~scanf("%s",b))
    {
        if(strcmp(b,"START")==0)continue;
        if(strcmp(b,"END")==0)break;
        scanf("%s",a);
        creat(a);
    }
    getchar();
    while(gets(c))
    {
        if(strcmp(c,"START")==0)continue;
        if(strcmp(c,"END")==0)break;
        memset(str,'\0',sizeof(str));
        for(int i=0;i