字典树——动态&&静态
静态---时间快
/*************************************************************************
> 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';
}
}
}
字典树——动态&&静态的更多相关文章
- SDUT OJ 字典树 AND 静态内存与动态内存
字典树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 遇到单词不认识怎么办? 查字典 ...
- Trie字典树 动态内存
Trie字典树 #include "stdio.h" #include "iostream" #include "malloc.h" #in ...
- IOS NS 字符串 数组 字典 文件 动态 静态 操作
ios 常用字符串的操作 //将NSData转化为NSString NSString* str = [[NSString alloc] initWithData:response e ...
- HDU:1251-统计难题(字典树模板,动态建树,静态建树)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memor ...
- HDU1247(经典字典树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 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 ...
- POJ3630/HDU-1671 Phone List,字典树静态建树!
Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...
- Trie字典树 静态内存
静态字典树 看了好久的字典树,挺简单的一个结构,愣是看了这么久才写出来... 专心一点就不会这样了.... 接下来就去刷刷字典树的题吧....... 下面是字典树.... 定义节点 typedef s ...
- poj 3007 Organize Your Train part II(静态字典树哈希)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...
随机推荐
- 通过Precision/Recall判断分类结果偏差极大时算法的性能
当我们对某些问题进行分类时,真实结果的分布会有明显偏差. 例如对是否患癌症进行分类,testing set 中可能只有0.5%的人患了癌症. 此时如果直接数误分类数的话,那么一个每次都预测人没有癌症的 ...
- Java-Logger日志
<转载于--https://www.cnblogs.com/yorickLi/p/6158405.html> Java中关于日志系统的API,在 java.util.logging 包中, ...
- 大众点评cat监控平台搭建
参考官方文档:https://github.com/dianping/cat/wiki/readme_server 1.数据库相关 (1)创建数据库cat,并执行以下sql创建相关表: CREATE ...
- docker(常见调试技巧):docker打包镜像调试技巧
写Dockerfile可以先不指定CMD.ENTRYPOINT等启动命令,只要拷贝就好了 如下: # Dockerfile for basic-app-client # Build with: # d ...
- Delphi中基本控件之SaveDialog控件的使用总结
首先向Form窗体拖一个SaveDialog控件,Name属性改为:dlgSave,然后添加一个按钮,Caption属性改为:浏览,Name属性改为:btnBrowse. 然后双击浏览按钮添加如下代码 ...
- 学习Go语言(一)环境安装及HelloWorld
自己开发的时候,一般用Java和C#居多,偶尔也用Python做点东东. 想体验一下比较“现代”语言,思来想去就来体验一下Go语言. 闲话少叙,言归正传,首先就是环境安装,这个轻车熟路: (1)到官网 ...
- sql回显注入-笔记
拼接sql命令查询数据 注释 常用于sql注入 # 井号 单行注释 注意:URL编码 %23 -- 两个减号加空格 单行注释 /* ...
- 使用graphics.h来绘制图形
| 版权声明:本文为博主原创文章,未经博主允许不得转载. graphics.h是TC里面的图形库,如果要用的话应该用TC来编译.分为:像素函数.直线和线型函数.多边形函数.填充函数等.然而在我们使 ...
- Linux上面执行 Windows 命令(比如 重启服务)的简单方法
1. 首先 基础是:openssh 还有 expect的包 2. 方法 安装openssh 转帖来自: https://www.jianshu.com/p/6e5bc39d386e 最近项目在搞Jen ...
- CAS导致的ABA问题及解决:时间戳原子引用AtomicReference、AtomicStampedReference
1.CAS导致ABA问题: CAS算法实现一个重要前提需要取出内存中某时刻的数据并在当下时刻比较并交换,那么在这个时间差中会导致数据的变化. 比如:线程1从内存位置V中取出A,这时线程2也从V中取出A ...