静态---时间快

/*************************************************************************
> File Name: Trie.c
> Author:
> Mail:
> Created Time: Tue 11 Dec 2018 03:46:05 PM CST
************************************************************************/ #include<stdio.h>
#include<string.h>
#include<stdlib.h> int charmapping[];
void init_charmapping()
{
for(int i='a';i<='z';i++)
{
charmapping[i]=i-'a';
}
} const int maxn = ; //这里假设字符串只出现26个小写字母
const int maxm = ; struct treenode
{
bool end;
struct treenode *next[maxn];
}head; struct treenode memory[maxm];
int mallocp = ; void init()
{
head.end = ;
for(int i = ;i<maxn;i++) head.next[i] = NULL;
} treenode* createnew()
{
treenode *newnode;
newnode = &memory[mallocp++];
newnode->end = ;
for(int i=;i<maxn;i++) newnode->next[i] = NULL;
return newnode;
} void update(char *s)
{
int k = ,temp;
treenode *t = &head;
while(s[k])
{
temp = charmapping[s[k]]; //找到c字符对应的标号2
if(!t->next[temp]) t->next[temp] = createnew();
t = t->next[temp];
k++;
}
t->end = ;
} bool search(char *s)
{
int k = ,temp;
treenode *t = &head;
while(s[k])
{
temp = charmapping[s[k]];
if(!t->next[temp]) return false; //已经遍历到最后一个结点
t = t->next[temp];
k++;
}
if(t->end) return true;
return false;
} int main(int argc,char **argv)
{
//freopen("text.txt","r",stdin);
init();
char x[];
char t;
while()
{
fflush(stdin);
scanf("%c",&t);
getchar();
if(t=='q')
{
scanf("%c",&x);
getchar();
if(search(x)) printf("匹配成功!\n");
else printf("匹配失败!\n");
}
else if(t=='u')
{
scanf("%s",&x);
getchar();
update(x);
printf("更新完毕!\n");
}
else if(t=='e')
{
printf("退出ing……\n");
break;
}
else
printf("无效命令!\n");
}
return ;
}

动态方法

/*************************************************************************
> File Name: Trie_dynamic.cpp
> Author:
> Mail:
> Created Time: Tue 11 Dec 2018 05:23:37 PM CST
************************************************************************/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std; int charmapping[];
void init_charmapping()
{
for(int i='a';i<'z';i++)
{
charmapping[i] = i - 'a';
}
} const int maxn = ;
const int maxm = ;
struct treenode
{
int count;
treenode *next[maxn];
}head; void init_trie()
{
head.count = ;
for(int i=;i<maxm;i++) head.next[i] = NULL;
} treenode* createnew()
{
treenode *newnode;
newnode = (treenode*)malloc(sizeof(treenode));
newnode->count = ;
for(int i=;i<maxn;i++) newnode->next[i] = NULL;
return newnode;
} void update(char *s,int num)
{
int k=,temp;
treenode *t = &head;
while(s[k])
{
t->count+=num;
temp = charmapping[s[k]];
if(!t->next[temp]) t->next[temp] = createnew();
t = t->next[temp];
k++;
}
t->count+=num;
} bool search(char *s,int num)
{
int k = ,temp;
treenode *t = &head;
while(s[k])
{
temp = charmapping[s[k]];
if(!t->next[temp] || t->next[temp]->count<num) return false;
t = t->next[temp];
k++;
}
int snum = t->count;
for(int i=;i<maxn;i++) if(t->next[i]) snum -= t->next[i]->count;
if(snum>=num) return true;
return false;
} void erase(char *s,int num)
{
int k = ,temp;
treenode *t = &head;
treenode *t1;
head.count -= num;
while(s[k])
{
temp = charmapping[s[k]];
t->next[temp]->count -= num;
if(t->next[temp]->count==)
{
t1 = t->next[temp];
t->next[temp]=NULL;
k++;
break;
}
t = t->next[temp];
k++;
}
while(s[k])
{
temp = charmapping[s[k]];
t = t1->next[temp];
free(t1);
t1 = t;
k++;
}
} char temp[];
void printall(treenode *tnode,int pos)
{
int count = tnode->count;
for(int i=;i<maxn;i++) if(tnode->next[i]) count -= tnode->next[i]->count;
for(int i=;i<count;i++) printf("\"%s\"\n",temp);
for(int i='a';i<='z';i++)
{
if(tnode->next[charmapping[i]])
{
temp[pos] = i;
temp[++pos]='\0';
printall(tnode->next[charmapping[i]],pos);
temp[--pos]='\0';
}
}
}

字典树——动态&&静态的更多相关文章

  1. SDUT OJ 字典树 AND 静态内存与动态内存

    字典树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 遇到单词不认识怎么办? 查字典 ...

  2. Trie字典树 动态内存

    Trie字典树 #include "stdio.h" #include "iostream" #include "malloc.h" #in ...

  3. IOS NS 字符串 数组 字典 文件 动态 静态 操作

    ios 常用字符串的操作   //将NSData转化为NSString        NSString* str = [[NSString alloc] initWithData:response e ...

  4. HDU:1251-统计难题(字典树模板,动态建树,静态建树)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memor ...

  5. HDU1247(经典字典树)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)

    Phone List 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Given a list of phone numbers, determine if it i ...

  7. POJ3630/HDU-1671 Phone List,字典树静态建树!

    Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...

  8. Trie字典树 静态内存

    静态字典树 看了好久的字典树,挺简单的一个结构,愣是看了这么久才写出来... 专心一点就不会这样了.... 接下来就去刷刷字典树的题吧....... 下面是字典树.... 定义节点 typedef s ...

  9. poj 3007 Organize Your Train part II(静态字典树哈希)

    Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...

随机推荐

  1. leetcode 658找到k个最接近的元素

    class Solution { public: vector<int> findClosestElements(vector<int>& arr, int k, in ...

  2. nodejs之express静态路由、ejs

    1.静态路由与ejs使用 /** *1.安装ejs npm install ejs --save-dev * *2.express 里面使用ejs ,安装以后就可以用,不需要引入 * *3.配置exp ...

  3. flutter flutter_swiper使用

    flutter_swiper flutter最强大的siwiper, 多种布局方式,无限轮播,Android和IOS双端适配. 更多详情信息请移步:https://blog.csdn.net/u011 ...

  4. mysql登录的三种方式

    1.远程登录mysql 先授权:如:grant all on *.* to 'root'@'192.168.81.130' identified by '52033dd';查看是否生效:select ...

  5. vue v-for直接循环数字

    <svg class="icon" aria-hidden="true" v-for="index of 5" :key=" ...

  6. ssh连接报错

    1.ssh: connect to host 172.16.0.142 port 22: Connection refused 解决: 在sshd 被连接端输入: [root@ecs-01 ~]# e ...

  7. PHP 异步执行方式

    在工作中我们经常遇到一些比较耗时的任务,比如用户注册发送邮件,审核短信通知等功能,同步执行这些功能的话,响应时间就会变长,所以一般我们会用队列去管理这些功能,但是如果条件不允许怎么办,今天get了一个 ...

  8. "fatal error LNK1169: 找到一个或多个多重定义的符号" 解决方案

    本人在测试刚刚安装的vs2017时运行出了问题, 错误信息为 "fatal error LNK1169: 找到一个或多个多重定义的符号", 代码如下: //Myfile.h #in ...

  9. 2019CSP-S游记(真)

    本来是考完了的,但是由于江西省的负责人员的不小心(?),江西oier的大部分代码都被删掉了, 所以我们需要重考,想看我之前CSP的游记可以看这个点我.下面是我江西重考的游记: Day0 又集训了一个星 ...

  10. c语言程序命名规范:函数、变量、数组、文件名

    函数: //send or recv data task void send_recv_data(void *pvParameters); //get socket error code. retur ...