HDU 1075 1247(字典树)

1075

/* 字典树 hdu 1075
   火星文翻译
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;
const int MAX = 26;

struct node
{
    int count ;//记录到此是否是某个火星词的结尾
    char s[15];//如果是某个火星词的结尾则存储对应的英文单词
    struct node *next[MAX];
};

struct node *root;

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

void Insert(char *ch,char *s)
{
    int len=strlen(s);
    if(len==0)return ;
    node *p;
    p = root;
    for(int i = 0; i < len; i++)
    {
        if(p->next[s[i]-'a']!=NULL)
        {
            p=p->next[s[i]-'a'];
        }
        else
        {
            p->next[s[i]-'a']=build();
            p=p->next[s[i]-'a'];
        }
    }
    p->count = 1;
    strcpy(p->s,ch);
}

int Query(char *s)
{
    struct node *p;
    int len = strlen(s);
    if(len==0)return 0;
    p = root;
    for(int i=0;i<len ;i++)
    {
        if(p->next[s[i]-'a']!=NULL)
            p=p->next[s[i]-'a'];
        else
            return 0;
    }
    if(p->count==1)
    {
        printf("%s",p->s);
        return 1;
    }
    return 0;//这个地方要注意,该单词有可能只是某个词的前缀
}
int main()
{
    char str[15],str1[15],str2[15],str3[3005];
    root = build();
    gets(str);
    while(scanf("%s",str1) && strcmp(str1,"END")!=0)
    {
        getchar();
        scanf("%s",str2);
        getchar();
        Insert(str1,str2);
    }
    getchar();
    gets(str2);
    gets(str3);
    while(strcmp(str3,"END")!=0)
    {
        int len = strlen(str3);
        for(int i = 0; i < len;)
        {
            if(str3[i] < 'a' || str3[i] > 'z')
            {
                printf("%c",str3[i]);
                i++;
            }
            else
            {
                int t = 0;
                while(str3[i]>='a'&&str3[i]<='z')
                {
                    str1[t++] = str3[i];
                    i++;
                }
                str1[t] = '\0';
                if(Query(str1)==0)printf("%s",str1);
            }
        }
        printf("\n");
        gets(str3);
    }
    return 0;
}

1247

/* 字典树 hdu 1247
   判断某个单词是不是由给出的单词表中的任意两个单词组合成的
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <algorithm>

using namespace std;
const int MAX = 26;

struct node
{
    int count ;//记录到此是否是某个词的结尾
    struct node *next[MAX];
};

struct node *root;

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

void Insert(char *s)
{
    int len=strlen(s);
    if(len==0)return ;
    node *p;
    p = root;
    for(int i = 0; i < len; i++)
    {
        if(p->next[s[i]-'a']!=NULL)
        {
            p=p->next[s[i]-'a'];
        }
        else
        {
            p->next[s[i]-'a']=build();
            p = p->next[s[i]-'a'];
        }
    }
    p->count = 1;
}

int Query(char *s)
{
    int len = strlen(s);
    node *p;
    p = root;
    stack<int > k;
    int i = 0;
    while(i < len)
    {
        if(p->next[s[i]-'a']==NULL)return 0;
        p = p->next[s[i]-'a'];
        if(p->count==1) //找到该单词含有子单词的分隔点
        {
            k.push(i);
        } //入栈
        i++;

    }
    while(!k.empty())
    {
        int i = k.top() + 1;
        k.pop();
        node *p = root;
        bool flag = true;
        while(i < len)
        {
            if(p->next[s[i]-'a']==NULL)
            {
                flag = false;
                break;
            }
            p = p->next[s[i]-'a'];
            i++;
        }
        if(flag && p->count==1)//找到最后,并且是单词的结尾
        return 1;
    }
    return 0;
}

//void Release(node *p)
//{
//    if(p==NULL)
//    return ;
//    for(int i = 0; i < MAX; i++)
//    {
//        if(p->next[i]!=NULL)
//        Release(p->next[i]);
//    }
//    free(p);
//    return ;
//}

int main()
{
    char m[50005][20];
    int i = 0;
    root = build();
    while(~scanf("%s",m[i]))
    {
        Insert(m[i++]);
    }
    for(int j = 0; j < i; j++)
    {
        if(Query(m[j])==1)
        puts(m[j]);
    }
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值