静态---时间快

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

热门文章

  1. leetcode 509斐波那契数列
  2. [flask_sqlalchemy ]插入数据时发生错误后如何处理
  3. Python 基础语法_Python脚本文件结构
  4. Selenium 2自动化测试实战9(简单元素操作)
  5. IDEA/Git 提交/commit 忽略 文件夹
  6. delphi dbgrid数据导出为excel
  7. pycharm修改代码后第一次运行不生效解决
  8. MySQL 常用工具sysbench/fio/tpcc等测试
  9. python学习之模块-模块(二)
  10. 2019JAVA第四次实验报告