HDU 1251 1671 (简单字典树)

/* 简单字典树 hdu 1251
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#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 = 1;//初始化
    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'];
            p->count++;

        }
        else
        {
            p->next[s[i]-'a'] = build();
            p = p->next[s[i]-'a'];
        }
    }
}

int Query(char *s)//查找
{
    int len = strlen(s);
    if(len==0) return 0;
    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 return 0;
    }
    return p->count;
}

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 str[11];
    root = build();
    while(gets(str)&&str[0]!='\0')
    Insert(str);
    while(~scanf("%s",str))
    {
        printf("%d\n",Query(str));
    }
    Release(root);
    return 0;
}

1671

/*字典树 hdu 1671
  判断在给出的n个字符串中是否存在一些字符串是其他字符串的子串
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;
const int MAX = 10;

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 < 10; i++)
    {
        p->next[i] = NULL;
    }
    p->count = -1;//初始化为-1
    return p;
}

int Insert(char *s)//插入
{
    int len = strlen(s);
    node *p;
    p = root;
    bool flag =  true;
    for(int i = 0; i < len; i++)
    {
        if(p->next[s[i]-'0']!=NULL)
        {
            if(i==len-1)flag = false;//该串是之前插入字符串的子串
            p = p->next[s[i]-'0'];
            if(p->count==1)//之前插入的字符串是该串的子串
            return 0;
        }
        else
        {
            p->next[s[i]-'0'] = build();
            p = p->next[s[i]-'0'];
        }
    }
    p->count = 1;
    if(flag)
    return 1;
    else 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 str[20];
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        getchar();
        root = build();
        bool flag = true;
        for(int i = 0; i < n; i++)
        {
            gets(str);
            if(flag)
            {
                if(Insert(str)==0)
                {
                    flag = false;
                }
            }
        }
        if(flag) puts("YES");
        else puts("NO");
        Release(root);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的影城管理系统,源码+数据库+论文答辩+毕业论文+视频演示 随着现在网络的快速发展,网上管理系统也逐渐快速发展起来,网上管理模式很快融入到了许多生活之中,随之就产生了“小徐影城管理系统”,这样就让小徐影城管理系统更加方便简单。 对于本小徐影城管理系统的设计来说,系统开发主要是采用java语言技术,在整个系统的设计中应用MySQL数据库来完成数据存储,具体根据小徐影城管理系统的现状来进行开发的,具体根据现实的需求来实现小徐影城管理系统网络化的管理,各类信息有序地进行存储,进入小徐影城管理系统页面之后,方可开始操作主控界面,主要功能包括管理员:首页、个人中心、用户管理、电影类型管理、放映厅管理、电影信息管理、购票统计管理、系统管理、订单管理,用户前台;首页、电影信息、电影资讯、个人中心、后台管理、在线客服等功能。 本论文主要讲述了小徐影城管理系统开发背景,该系统它主要是对需求分析和功能需求做了介绍,并且对系统做了详细的测试和总结。具体从业务流程、数据库设计和系统结构等多方面的问题。望能利用先进的计算机技术和网络技术来改变目前的小徐影城管理系统状况,提高管理效率。 关键词:小徐影城管理系统;Spring Boot框架,MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值