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图的更多相关文章

  1. 笔试算法题(39):Trie树(Trie Tree or Prefix Tree)

    议题:TRIE树 (Trie Tree or Prefix Tree): 分析: 又称字典树或者前缀树,一种用于快速检索的多叉树结构:英文字母的Trie树为26叉树,数字的Trie树为10叉树:All ...

  2. hiho一下 第二周 trie树

    Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路 ...

  3. 编程之美--2. Trie树 (Trie图)

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

  4. 双数组Trie树 (Double-array Trie) 及其应用

    双数组Trie树(Double-array Trie, DAT)是由三个日本人提出的一种Trie树的高效实现 [1],兼顾了查询效率与空间存储.Ansj便是用DAT(虽然作者宣称是三数组Trie树,但 ...

  5. hihoCoder 1014 Trie树 (Trie)

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...

  6. hiho一下第二周 Trie树

    题目链接:http://hihocoder.com/problemset/problem/1014 #include <iostream> #include <cstdio> ...

  7. hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)

    思路: 完全看题目中的介绍就行了.还有里面的input写道:不保证是英文单词,也有可能是火星文单词哦.比赛结束后的提交是不用考虑26个字母之外的,都会AC,如果考虑128种可能的话,爆了内存.步骤就是 ...

  8. 【hiho一下第二周 】Trie树

    [题目链接]:http://hihocoder.com/problemset/problem/1014 [题意] [题解] 在字典树的域里面加一个信息cnt; 表示这个节点下面,记录有多少个单词; 在 ...

  9. hihocoder_1014: Trie树(Trie树模板题)

    题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...

随机推荐

  1. EXISTS语句的子查询

    一.EXISTS运算符简介: 使用EXISTS语句可以测试集合是否为空,EXISTS语句通常与子查询结合在一起使用.只要子查询中至少返回一个值,则EXISTS语句的值就为True.EXISTS子查询的 ...

  2. 【mybatis】in查询+判断list查询条件是否进行in查询

    mybatis中的in查询: 并且判断in查询的list是否为null或者list有值才进行In查询 <select id="find" parameterType=&quo ...

  3. sed命令使用示例

    sed -i '/mirrorlist/d' CentOS-Base-163.repo 把有mirrorlist的行删除sed -i '/\[addons\]/,/^$/d' CentOS-Base- ...

  4. hdu2846 Repository

    //--------------------------------------------------------------- /*---字典树应用问题.考虑到要查询的次数在10^6,显然直接插入 ...

  5. Hadoop之HDFS详解

    1.HDFS的概念和特性 它是一个文件系统,其次是分布式的 重要特性: 1).HDFS中的文件在物理上是分块存储(block),新版默认128M 2).客户端通过路径来访问文件,形如:hdfs://n ...

  6. 2016.6.30 java.util.concurrent.ExecutionException java.lang.OutOfMemoryError

    选中ccs项目后,选择debug on server,但是运行到一半,跳出错误: java.util.concurrent.ExecutionException: java.lang.OutOfMem ...

  7. OpenCV2学习笔记(十五):利用Cmake高速查找OpenCV函数源代码

    在使用OpenCV时,在对一个函数的调用不是非常了解的情况下,通常希望查到该函数的官方声明.而假设想进一步研究OpenCV的函数,则必须深入到源码. 在VS中我们能够选中想要查看的OpenCV函数,点 ...

  8. Android源代码下载

    清华大学AOSP镜像: https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/

  9. 类的成员函数指针和mem_fun适配器的用法

    先来看一个最简单的函数: void foo(int a) { cout << a << endl; } 它的函数指针类型为 void (*)(int); 我们可以这样使用: v ...

  10. epoll使用详解(精髓)(转)

    epoll - I/O event notification facility 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是 ...