hdu1075 字典树

题意即把火星文翻译成英文,先给出对应关系,然后输入要查询的火星文

首先还是构造字典树,把火星文存进去,不同的是在每个节点开一个存字符串的数组,每存完一段火星文都把对应的英文存在最后那个结点

然后查询的时候分情况,标点符号直接输出,其他的到query函数去查,如果后面没有了,输出后返回,如果有对应单词,输出对应dic,如果没有输出本身

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>

using namespace std;

struct node{
    char dic[15];
    node * next[26];
    bool flag;
}*root;

node *build()
{
    node *p=(node *)malloc(sizeof(node));
    for(int i=0;i<26;i++)
        p->next[i]=NULL;
    p->flag=false;
    return p;
}

void insert(char *earth,char *mars)
{
    int len=strlen(mars);
    node *p;
    p=root;
    for(int i=0;i<len;i++)
    {
        if(p->next[mars[i]-'a']==NULL)
            p->next[mars[i]-'a']=build();
        p=p->next[mars[i]-'a'];
    }
    p->flag=true;
    strcpy(p->dic,earth);
}

void query(char *earth)
{
    int len=strlen(earth);
    node *p;
    p=root;
    for(int i=0;i<len;i++)
    {
        if(p->next[earth[i]-'a']==NULL)
        {
            printf("%s",earth);
            return;
        }
        p=p->next[earth[i]-'a'];
    }
    if(p->flag)
        printf("%s",p->dic);
    else
        printf("%s", earth);
}

int main()
{
    char earth[15],mars[15],ask[3010];


    scanf("%s",earth);
    root=build();
    while(scanf("%s",earth),strcmp(earth,"END"))
    {
        scanf("%s",mars);
        insert(earth,mars);
    }


    scanf("%s",earth);
    getchar();
    while(gets(ask),strcmp(ask,"END"))
    {
        int len=strlen(ask);
        for(int i=0;i<len;i++)
        {
            if(islower(ask[i]))
            {
                int j=0;
                memset(earth,'\0',sizeof(earth));
                while(islower(ask[i]))
                    earth[j++]=ask[i++];
                query(earth);
            }
            if(!islower(ask[i]))
                printf("%c",ask[i]);
        }
        printf("\n");
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值