zoukankan      html  css  js  c++  java
  • 算法竞赛模板 字典树

    ①找符合某前缀的字符串个数

    #include<bits/stdc++.h>
    #define MAX 26
    using namespace std;
    typedef struct Node{
        int sum;
        Node*next[MAX];
        Node():sum(0){}
    };
    //初始化
    void init(Node*node)
    {
        for(int i=0;i<MAX;i++)
            node->next[i]=NULL;
    }
    //插入单词
    void Insert(Node*root,char*ch)
    {
        Node*p=root;
        while(*ch)
        {
            int index=*ch-'a';
            if(p->next[index]==NULL)
            {
                p->next[index]=new Node;
                init(p->next[index]);
            }
            p=p->next[index];
            p->sum++;
            ch++;
        }
    }
    //建树
    Node*TrieCreate()
    {
        char str[55];
        Node*root=new Node;
        init(root);
        while(gets(str)&&strlen(str))
        {
            Insert(root,str);
        }
        return root;
    }
    //查找
    int TrieSearch(Node*root,char*ch)
    {
        Node*p=root;
        int sum=0;
        while(*ch)
        {
            int index=*ch-'a';
            if(p->next[index]==NULL)
                return 0;
            p=p->next[index];
            sum=p->sum;
            ch++;
        }
        return sum;
    }
    //释放空间
    void Free(Node*root)
    {
        for(int i=0;i<MAX;i++)
            if(root->next[i]!=NULL)
                Free(root->next[i]);
        free(root);
    }
    int main()
    {
        char str[55];
        int num=0;
        Node*root=TrieCreate();
        while(scanf("%s",str)!=EOF)
        {
            cout<<TrieSearch(root,str)<<endl;
        }
        Free(root);
        return 0;
    }

    ②找是否存在一个字符串是由其他两个字符串拼接而成,若有则输出

    #include<bits/stdc++.h>
    #define MAX 26
    using namespace std;
    char s[50005][55];
    int num;
    typedef struct Node{
        bool ok;
        Node*next[MAX];
    };
    //初始化
    void init(Node*node)
    {
        for(int i=0;i<MAX;i++)
            node->next[i]=NULL;
    }
    //插入单词
    void Insert(Node*root,char*ch)
    {
        Node*p=root;
        while(*ch)
        {
            int index=*ch-'a';
            if(p->next[index]==NULL)
            {
                p->next[index]=new Node;
                init(p->next[index]);
                p->next[index]->ok=false;
            }
            p=p->next[index];
            ch++;
        }
        p->ok=true;
    }
    //建树
    Node*TrieCreate()
    {
        char str[55];
        num=0;
        Node*root=new Node;
        init(root);
        while(gets(str)&&strlen(str))
        {
            strcpy(s[num++],str);
            Insert(root,str);
        }
        return root;
    }
    //查找
    bool TrieSearch(Node*root,char*ch)
    {
        Node*p=root;
        while(*ch)
        {
            int index=*ch-'a';
            if(p->next[index]==NULL||!p)
                return false;
            p=p->next[index];
            ch++;
        }
        return p->ok;
    }
    //释放空间
    void Free(Node*root)
    {
        for(int i=0;i<MAX;i++)
            if(root->next[i]!=NULL)
                Free(root->next[i]);
        free(root);
    }
    int main()
    {
        char str[55];
        int i,j;
        Node*root=TrieCreate();
        for(i=0;i<num;i++)
        {
            for(j=1;j<strlen(s[i]);j++)
            {
                char str1[55]={''},str2[55]={''};
                strncpy(str1,s[i],j);
                strncpy(str2,s[i]+j,strlen(s[i])-j);
                if(TrieSearch(root,str1)&&TrieSearch(root,str2))
                {
                    printf("%s
    ",s[i]);
                    break;
                }
            }
        }
        Free(root);
        return 0;
    }
  • 相关阅读:
    linux 常用命令
    基于 DirectX11 的 MMDViewer 01-简介
    基于 DirectX11 的 MMDViewer 04-渲染目标视图和多视口
    基于 DirectX11 的 MMDViewer 03-渲染管线
    基于 DirectX11 的 MMDViewer 02-创建一个窗口
    Simple2D-26 Simple2D 最后的工作,开发结束
    Simple2D-25 精灵动作
    Simple2D-24 Sprite 渲染树
    Simple2D-23(重构)反走样几何图形
    Simple2D-22(重构)纹理池
  • 原文地址:https://www.cnblogs.com/kannyi/p/9114011.html
Copyright © 2011-2022 走看看