字典树——动态&&静态
2024-10-07 06:02:48
静态---时间快
/*************************************************************************
> 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';
}
}
}
最新文章
- JS日历制作获取时间
- 计算excel列的名字
- iOS开发--git
- 好用的linux命令
- Some General concepts in ISO C
- hdu2460-Network:边的双连通分量
- 初识C(1)----与C基本无关的开篇
- 原生JS+Canvas实现五子棋游戏
- 关于ios::sync_with_stdio(false)
- UEP-添加表格
- 一个CSS+jQuery的放大缩小动画效果
- python基础知识5---数据类型、字符编码、文件处理
- 红米Note5进入全网通5.0时代,其实是高通已经落后了!
- file 上传大小限制问题
- Ubuntu安装VirtualBox以及CentOS7.5联网设置
- shell和shell脚本基本知识
- ScheduledThreadExecutor定时任务线程池
- HTML布局思路
- 算法-Java组合
- jquery实现全选,取消,反选的功能&;实现左侧菜单