字典树模板(非原创)

动态版本

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
const int maxn = 15;
int ans;
char num[10000];


struct Trie{
    struct Trie* next[maxn];
    bool flag;
};

struct Trie* root;

struct Trie* Creat_Trie(){
    struct Trie* temp = (struct Trie*)malloc( sizeof(struct Trie) );
    for(int i = 0 ; i < 10 ; i++){
        temp -> next[i] = NULL;
    }
    temp -> flag = false;
    return temp;
}

void del(Trie *rt){         //Delete重名
     if(!rt) return;
     for(int i = 0 ; i < 10 ; i++){
        if(rt->next[i]) del(rt->next[i]);
     }
     free(rt);
}

void Insert(char* s){
    int len = strlen(s);
    struct Trie* temp = root;
    bool ff = false;
    for(int i = 0 ; i < len ; i++){
        //printf("i = %d\n",i);
        int k = s[i] - '0';
        if( temp -> next[k] ){
            if(!temp -> flag && i == len - 1){    //当前字符串为其他字符串的子串。
                ans++;
                ff = true;
            }
            temp = temp -> next[k];
            if( i == len - 1 ) temp -> flag = true;
            if(ff) return ;
        }
        else{
            if(temp -> flag){   //说明找到这里已经有某个串是该串的子串了,不需要向下创建。
                ans = true;
                return;
            }
            else{
                temp -> next[k] = Creat_Trie();
                temp = temp -> next[k];
                if(i == len - 1) temp -> flag = true;  //如果该串已经到了结尾,那么对应的尾节点flag 正确代表一个字符串,
                else temp -> flag = false;
            }
        }
    }
}


int main(){
    int cas;
    cin >> cas;
    while(cas--){
        int n;
        ans = false;
        cin >> n;
        root = Creat_Trie();
        while(n--){
            cin >> num;
            Insert(num);
        }
        del(root);
        if(!ans) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}

静态版本(更快)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
#define LL long long
#define CLR(x) memset(x, 0, sizeof(x))
struct Trie{
    int cnt;
    Trie *next[26];
};
Trie Merrory[1000000];
int ant;
Trie *Create()
{
    Trie *rt;
    rt = &Merrory[ant++];
    rt->cnt = 1;
    for (int i = 0; i < 26; i++)
        rt->next[i] = NULL;
    return rt;
}

void Insert(char *s, Trie *Prt)
{
    Trie *p;
    if(!(p = Prt)) p = Prt = Create();      //括号里面只要能有一个等号嘛= =

    int i = 0, k;
    while(s[i])
    {
        k = s[i++] - 'a';
        if(p->next[k]) p->next[k]->cnt++;
        else p->next[k] = Create();

        p = p->next[k];
    }
}

int Search(char *s, Trie *rt)
{
    if (!rt) return 0;
    Trie *tmp = rt;
    int i = 0, k;
    while(s[i])
    {
        int k = s[i] - 'a';
        if (tmp->next[k]) tmp = tmp->next[k];
        else return 0;
        i++;
    }
    return tmp->cnt;
}

int main()
{
    char s[12]={0};
    Trie *rt = Create();
    while(gets(s) && s[0]!='\0')
    {
        Insert(s, rt);
    }
    while(gets(s))
    {
        int res = Search(s, rt);
        printf("%d\n", res);
    }
    return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、温故而知新 1. 内存不够怎么办 内存简单分配策略的问题地址空间不隔离内存使用效率低程序运行的地址不确定 关于...
    SeanCST阅读 7,889评论 0 27
  • 静态库与动态库的区别 首先来看什么是库,库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别...
    吃瓜群众呀阅读 12,101评论 3 42
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 家事一则 家中娇儿电, 切诉交往难。 夫回学为重, 大呼心儿酸。 近日景 假日如花开, 流光似水湍。 每日邀随笔,...
    火红的石榴暖暖阅读 182评论 0 0
  • 今年,有篇关于过年必须有仪式感的文章很火,大意是一些习俗的延续,仪式,在现代社会里,还有存在的必要性吗?。 在阿公...
    噹噹FUNG阅读 251评论 0 0