静态---时间快

/*************************************************************************
> 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. struts1使用select标签

    今天使用struts1标签的时候总是出错,后来查了一下,好像不能和什么标签混用.就只用了html原来的标签实现 <select name="newsType_id"> ...

  2. Centos 7 安装tomcat并部署jar实录

    本文目的 纯属记录,以备后查. 1.安装JAVA JDK 安装jdk略. 配置JDK,打开/etc/profile文件,在最后添加如下代码: JAVA_HOME=/usr/java/jdk1..0_2 ...

  3. ALV程序设计

    ALV 全称SAP LIST VIEW, 是SAP所提供的一个强大的数据报表显示工具. ALV显示格式分为GRID及LIST两种,两者所显示数据一致, GRID模式在每个输出字段提供选择按钮,允许用 ...

  4. [LeetCode] 373. Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  5. java:Spring框架2(bean的作用域,静态工厂和实例工厂,自动装配,动态代理)

    1.bean的作用域,静态工厂和实例工厂: bean.xml: <?xml version="1.0" encoding="UTF-8"?> < ...

  6. 微信小程序前端支付

    原文地址 //index.js Page({ data: { }, //点击支付按钮进行支付 payclick: function () { var t = this; wx.login({ //获取 ...

  7. APP Store上架QA&注意事项

    一. App Store上架费用,要多少钱. 这个因产品而异,一般是6000-10000元人民币. 二. App Store上架周期,要多久过. 这个因产品而异,正常的话一周内,如果产品老是出问题,被 ...

  8. Python unittest 之 BeautifulReport可视化报告

    众所周知的报告是HTMLTestRunner,虽然经过众多的大神修改后,功能挺强大的,但这颜值,我就不多说了,大家自己感受下吧 HTMLTestRunner就不多说了,近来发现了一款款式新颖,还不漏油 ...

  9. ugui多层解决方案

    创建不同的相机图层以渲染事物(最佳实践解决方案) 链接:https://answers.unity.com/questions/1169898/hi-guys-i-need-some-help-wit ...

  10. 【Deep Learning Nanodegree Foundation笔记】第 7 课:NEURAL NETWORKS Intro to Neural Networks

    In this lesson, you'll dive deeper into the intuition behind Logistic Regression and Neural Networks ...