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): 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('\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.


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

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<strlen(c);i++)
        {
            if(islower(c[i]))//如果是小写字母,
            {
                int cont=0;
                int k=i;
                while(k<strlen(c)&&islower(c[k]))//用str字符串数组截取下来这个单词、
                {
                    str[cont]=c[k];
                    k++;cont++;
                }
                str[cont]='\0';//这里别忘记了、
                if(find(str)==0)//如果没能找到映射部分单词
                {
                    for(int j=i;j<=k-1;j++)//原样输出
                    {
                        printf("%c",c[j]);
                    }
                }
                i=k-1;//i向后移动
            }
            else//如果不是小写字母,原样输出、
            {
                printf("%c",c[i]);
            }
        }
        printf("\n");
    }
然后强调一下刚才说的第三点如何处理、字典树的作用是查找前缀、如果只找到了前缀就输出的话,是一定会wa的、所以我们这里在初始化的时候,加上一个标记变量,在creat的时候的最后一个字母处给标记赋值,当find的时候 ,如果查到的地方的标记有赋值,那么就输出,否则就相当于没有找到、

同时,在给标记赋值的同时,给节点strcpy一下映射的单词、

这里贴上处理代码:

void creat(char *str)
{
    int len=strlen(str);
    tree *p=&root,*q;
    for(int i=0;i<len;i++)
    {
        int id=str[i]-'a';
        if(p->next[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;i<len;i++)
    {
        int id=str[i]-'a';
        p=p->next[id];
        if(p==NULL)
        return 0;
    }
    if(p->flag==1)//光找到前缀是不行的。
    printf("%s",p->la);
    else
    return 0;
    return 1;
}
然后是完整的AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
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;i<len;i++)
    {
        int id=str[i]-'a';
        if(p->next[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;i<len;i++)
    {
        int id=str[i]-'a';
        p=p->next[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<strlen(c);i++)
        {
            if(islower(c[i]))
            {
                int cont=0;
                int k=i;
                while(k<strlen(c)&&islower(c[k]))
                {
                    str[cont]=c[k];
                    k++;cont++;
                }
                str[cont]='\0';
                if(find(str)==0)
                {
                    for(int j=i;j<=k-1;j++)
                    {
                        printf("%c",c[j]);
                    }
                }
                i=k-1;
            }
            else
            {
                printf("%c",c[i]);
            }
        }
        printf("\n");
    }
}
/*
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
*/







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值