hiho一下 第二周&第四周:从Trie树到Trie图
hihocoder #1014 题目地址:http://hihocoder.com/problemset/problem/1014
hihocoder #1036 题目地址: http://hihocoder.com/problemset/problem/1036
trie图其实就是trie树+KMP
#1014trie树
#include<stdio.h>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cstdio>
#include <cmath> using namespace std; typedef struct Trie_node
{
int count; // 统计单词前缀出现的次数
struct Trie_node* next[]; // 指向各个子树的指针
bool exist; // 标记该结点处是否构成单词
}TrieNode , *Trie; Trie createTrieNode()
{
TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode));
node->count = ;
node->exist = false;
memset(node->next , , sizeof(node->next)); // 初始化为空指针
return node;
} void Trie_insert(Trie root, char* word)
{
Trie node = root;
char *p = word;
int id;
while( *p )
{
id = *p - 'a';
if(node->next[id] == NULL)
{
node->next[id] = createTrieNode();
}
node = node->next[id];
++p;
node->count += ; // 包括统计每个单词出现的次数
}
node->exist = true; // 可以构成一个单词
} int Trie_search(Trie root, char* word)
{
Trie node = root;
char *p = word;
int id;
while( *p )
{
id = *p - 'a';
node = node->next[id];
++p;
if(node == NULL)
return ;
}
return node->count;
} int main()
{
Trie root = createTrieNode(); // 字典树的根节点
char str[] ;
bool flag = false;
int n ,m ;
scanf ("%d", &n);
for( int i = ; i < n ; i++)
{
scanf ("%s", str);
Trie_insert(root , str);
}
scanf ("%d", &m);
for( int i = ; i < m ; i++)
{
scanf ("%s", str);
printf("%d\n",Trie_search(root , str));
}
return ;
}
#1036trie图
其实就是trie树+KMP
数据结构与trie树一样,加了一个prev指针,作用类似于KMP的失配函数next[]
Trie_insert函数不变
添加一个构造prev的函数Trie_build()。
prev指针的作用:在匹配失败时跳转到具有公共前缀的字符继续匹配,类似于KMP的失配函数next[]。
利用bfs构造prev指针。
指针prev指向与字符p相同的结点,如果没有与p前缀相同的节点,则指向root
根节点的前缀是根节点
最后字符匹配的Trie_search()函数类似于KMP的过程,在当前字符匹配失败时,利用prev指针跳转到具有最长公共前后缀的字符继续匹配。
#include<stdio.h>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath> using namespace std; typedef struct Trie_node
{
int count; // 统计单词前缀出现的次数
struct Trie_node* next[];
bool exist; // 标记该结点处是否构成单词
struct Trie_node* prev; //前缀节点
}TrieNode , *Trie; Trie createTrieNode()
{
TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode));
node->prev=NULL;
node->count = ;
node->exist = false;
memset(node->next , , sizeof(node->next));
return node;
} void Trie_insert(Trie root, char* word)
{
Trie node = root;
char *p = word;
int id;
while( *p )
{
id = *p - 'a';
if(node->next[id] == NULL)
{
node->next[id] = createTrieNode();
}
node = node->next[id];
++p;
node->count += ; // 统计每个单词出现的次数
}
node->exist = true; // 单词结束的地方标记
} void Trie_build(Trie root) //Trie树和Tie图的区别就在于此,类似于KMP构造失配函数的一个过程
{
queue<Trie> Q; //利用bfs构造prev指针,队列实现BFS
Trie node=root;
for(int i=;i<;i++)//根节点的子节点的rev都是根节点,根节点的prev也是根节点
{
if(node->next[i]!=NULL)
{
node->next[i]->prev=root;
Q.push(node->next[i]);
}
}
while(!Q.empty())
{
node=Q.front();
Q.pop();
for(int i=; i<; i++)
{
Trie p=node->next[i];
if(p!=NULL&&p->exist==false) //若此处能构成单词则不用处理prev
{
Trie prev=node->prev; //上一个结点的前缀节点
while(prev)
{
if(prev->next[i]!=NULL)
{
p->prev=prev->next[i]; //prev指向与字符p相同的结点
if(p->prev->exist==true)
p->exist=true;
break;
}
else
prev=prev->prev;
} if(p->prev==NULL)//如果没有与p前缀相同的节点,则指向root
p->prev=root;
Q.push(p);
}
}
}
} bool Trie_search(Trie root, char* word)
{
Trie node = root;
char *p = word;
int id;
while( *p )
{
id = *p - 'a';
while(true)
{
if(node->next[id]!=NULL) //匹配成功
{
node = node->next[id];
if(node->exist)
return true;
break;
}
else node=node->prev; //类似KMP的失配过程,在当前字符匹配失败时,跳转到具有最长公共前后缀的字符继续匹配
if(node==root||node==NULL){
node=root;
break;
}
}
p++;
}
return false;
} char str[] ;
int main()
{
Trie root = createTrieNode(); // 初始化字典树的根节点
bool flag = false;
int n ;
scanf ("%d", &n);
for( int i = ; i < n ; i++)
{
scanf ("%s", str);
Trie_insert(root , str);
}
Trie_build(root);
scanf ("%s", str);
if(Trie_search(root , str)) printf("YES\n");
else printf("NO\n");
return ;
}
hiho一下 第二周&第四周:从Trie树到Trie图的更多相关文章
- 笔试算法题(39):Trie树(Trie Tree or Prefix Tree)
议题:TRIE树 (Trie Tree or Prefix Tree): 分析: 又称字典树或者前缀树,一种用于快速检索的多叉树结构:英文字母的Trie树为26叉树,数字的Trie树为10叉树:All ...
- hiho一下 第二周 trie树
Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路 ...
- 编程之美--2. Trie树 (Trie图)
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...
- 双数组Trie树 (Double-array Trie) 及其应用
双数组Trie树(Double-array Trie, DAT)是由三个日本人提出的一种Trie树的高效实现 [1],兼顾了查询效率与空间存储.Ansj便是用DAT(虽然作者宣称是三数组Trie树,但 ...
- hihoCoder 1014 Trie树 (Trie)
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...
- hiho一下第二周 Trie树
题目链接:http://hihocoder.com/problemset/problem/1014 #include <iostream> #include <cstdio> ...
- hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)
思路: 完全看题目中的介绍就行了.还有里面的input写道:不保证是英文单词,也有可能是火星文单词哦.比赛结束后的提交是不用考虑26个字母之外的,都会AC,如果考虑128种可能的话,爆了内存.步骤就是 ...
- 【hiho一下第二周 】Trie树
[题目链接]:http://hihocoder.com/problemset/problem/1014 [题意] [题解] 在字典树的域里面加一个信息cnt; 表示这个节点下面,记录有多少个单词; 在 ...
- hihocoder_1014: Trie树(Trie树模板题)
题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...
随机推荐
- RMAN BACKUP
转自 RMAN BACKUP backup terminology Using the RMAN BACKUP Command to Create Backups Server-Managed Con ...
- Linux文件压缩与解压命令
1 .zip 格式压缩与解压 压缩命令 zip 压缩文件名 源文件 zip -r 压缩目录名 源目录 解压命令 unzip 文件名 td@td-Lenovo-IdeaPad-Y41 ...
- CURL简单使用
学习地址:https://yq.aliyun.com/articles/33262 curl的简单使用步骤 要使用cURL来发送url请求,具体步骤大体分为以下四步: 1.初始化2.设置请求选项3.执 ...
- docker运行mysql
http://blog.csdn.net/u011492260/article/details/77970445 第一步: 安装Docker:首先到docker官网下载适合自己电脑当前系统的版本,并安 ...
- leetcode题解:Valid Palindrome(判断回文)
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- sersync+rsync实时数据同步
sersync+rsync实时数据同步 1.相关背景介绍 前面有关文章配置实现了rsync增量同步以及配置为定时同步,但是在实际生产环境中需要实时的监控数据从而进行同步(不间断同步),可以采取inot ...
- 使用Spring开发和监控线程池服务
第1步:添加maven 项目 第2步:添加依赖库 将Spring的依赖添加到Maven的pom.xml文件中. 1 2 3 4 5 6 7 8 9 10 11 <!-- Spring 3 dep ...
- Elasticsearch教程(六) elasticsearch Client创建
Elasticsearch 创建Client有几种方式. 首先在 Elasticsearch 的配置文件 elasticsearch.yml中.定义cluster.name.如下: cluster ...
- 资深程序员教你如何实现API自动化测试平台!附项目源码!
原文链接: 1.平时测试接口,总是现写代码,对测试用例的管理,以及测试报告的管理持久化做的不够, 2.工作中移动端开发和后端开发总是不能并行进行,需要一个mock的依赖来让他们并行开发. 3.同时让自 ...
- jdk/java版本与Android源码编译中的错误
错误一:javap未指向有效的java版本 Traceback (most recent call last): File "../../base/android/jni_generator ...