trie基本用法

2024-06-15 04:08
文章标签 用法 基本 trie

本文主要是介绍trie基本用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

poj2001 poj2503 poj3630poj1056poj1451

1.单词查找树

我们需要将一个确定的单词列表建出一棵单词查找树,满足
1. 根结点不包含字母,除根结点外的每个都仅含一个大写英文
字母;
2. 从根结点到某一结点,路径上经过的字母依次连起来所构成
的字母序列,称为该结点对应的单词。单词列表中的每个词,都是
该单词查找树某个结点所对应的单词;
3. 在满足上述条件下,该单词查找树的结点数最少。
统计出该单词查找树的结点数目。
输入格式
若干行单词,描述单词列表。
输出格式
一个数字,表示结点数目。
样例输入
A
AN
ASP
AS
ASC
ASCII

BASIC

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
using namespace std;
char ch[100];
struct Node
{
int next[26];
}temp[100000];
int num;
void build(char *s)
{
int len = strlen(s);
int p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - 'A';
if (!temp[p].next[t])
{
temp[p].next[t] = ++num;
}
p = temp[p].next[t];
}
}
void input()
{
int n;
while (cin >> n, n)
{
num = 0;
memset(temp, 0, sizeof(temp));
for (int i = 0; i < n; i++)
{
scanf("%s", ch);
build(ch);
}
cout << num + 1 << endl;
}
}
int main()
{
input();
return 0;
}


 

2.问所给单词中是否有的单词有公共前缀 hdu1671

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
using namespace std;
int n;
char ch[10000 + 1][20];
struct Node
{
int next[10];
int num;
}temp[100000 + 1];
bool ok;
int num;
void build(char *s)
{
int len = strlen(s), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - '0';
if (!temp[p].next[t])
{
temp[p].next[t] = ++num;
}
p = temp[p].next[t];
temp[p].num++;
}
}
void dfs(char *s)
{
int len = strlen(s), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - '0';
if (temp[p].next[t])
{
p = temp[p].next[t];
}
}
if (temp[p].num > 1)
{
ok = false;
}
}
void input()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
num = 0;
memset(temp, 0, sizeof(temp));
for (int i = 0; i < n; i++)
{
scanf("%s", ch[i]);
build(ch[i]);
}
ok = true;
for (int i = 0; i < n; i++)
{
if (ok) dfs(ch[i]);
}
printf("%s\n", ok ? "YES" : "NO");
}
}
int main()
{
input();
return 0;
}


3.判断一个单词是否在单词表中,不在输出并加入单词表

#include <iostream>
#include <string.h>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Node
{
int next[26];
int arrive;
}temp[100000];
char ch[100];
int num;
void build(char *s)
{
int len = strlen(s), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - 'a';
if (!temp[p].next[t])
{
temp[p].next[t] = ++num;
}
p = temp[p].next[t];
}
temp[p].arrive = 1;
}
bool dfs(char *s)
{
int len = strlen(s), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - 'a';
if (temp[p].next[t])
{
p = temp[p].next[t];
}
else
{
return false;
}
}
if (temp[p].arrive)
{
return true;
}
else
{
return false;
}
}
void input()
{
int n;
cin >> n;
num = 0;
memset(temp, 0, sizeof(temp));
for (int i = 0; i < n; i++)
{
scanf("%s", ch);
build(ch);
}
while (true)
{
scanf("%s", ch);
if (!dfs(ch))
{
cout << ch << endl;
build(ch);
}
}
}
int main()
{
input();
return 0;
}



 4.单词排序后输出

#include <iostream>
#include <string.h>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Node
{
int next[26];
int arrive;
}temp[100000];
char ch[100];
int num;
void build(char *s)
{
int len = strlen(s), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - 'a';
if (!temp[p].next[t])
{
temp[p].next[t] = ++num;
}
p = temp[p].next[t];
}
temp[p].arrive++;
}
void dfs(int p, char s[], int len)
{
while (temp[p].arrive--)
{
cout << s << endl;
}
for (int i = 0; i < 26; i++)
{
if (temp[p].next[i])
{
s[len++] = i + 'a';
dfs(temp[p].next[i], s, len);
len--;
}
}
}
char chs[1000];
void input()
{
int n;
cin >> n;
num = 0;
memset(temp, 0, sizeof(temp));
for (int i = 0; i < n; i++)
{
scanf("%s", ch);
build(ch);
}
dfs(0, chs, 0);
}
int main()
{
input();
return 0;
}


 

5文章解密
给出有 N 个单词的字典,和一篇长 L 的没有标点符号的文章。问这文章
有多少种解释方式。
输入格式
第一行,一个数字 N;
接下来 N 行,每行一个单词,描述字典内容;
接下来若干字符,描述文章。
输出格式
一个数字,表示所求答案并对 12345679 取模。
样例输入
5
ab
cb
bc
ba
a
abcba
样例输出
2

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
using namespace std;
struct Node
{
int next[26];
int arrive;
}temp[100000];
char ch[100];
int num;
void build(char *s)
{
int len = strlen(s), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - 'a';
if (!temp[p].next[t])
{
temp[p].next[t] = ++num;
}
p = temp[p].next[t];
}
temp[p].arrive = 1;
}
bool dfs(string &s)
{
int len = s.length(), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - 'a';
if (temp[p].next[t])
{
p = temp[p].next[t];
}
else
{
return false;
}
}
if (temp[p].arrive)
{
return true;
}
else
{
return false;
}
}
int f[1000];
void input()
{
int n;
cin >> n;
memset(temp, 0, sizeof(temp));
memset(f, 0, sizeof(f));
for (int i = 0; i < n; i++)
{
scanf("%s", ch);
build(ch);
}
scanf("%s", ch);
int len = strlen(ch);
f[0] = 1;
for (int i = 0; i < len; i++)
{
for (int j = i; j < len; j++)
{
string s = "";
for (int k = i; k <= j; k++)
{
s += ch[k];
}
if (dfs(s))
{
f[i + s.length()] += f[i];
}
}
}
for (int i = 0; i <= len; i++)
{
cout << f[i] << ' ';
}
cout << endl;
cout << f[len] << endl;
}
int main()
{
input();
return 0;
}


 al3942

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
using namespace std;
int temp[400000 + 5][26];
int val[400000 + 5];
char ch[100 + 5];
int num;
int f[300000 + 5];
char h[300000 + 5];
int l;
void build(char *s)
{
int len = strlen(s), p = 0;
for (int i = 0; i < len; i++)
{
int t = s[i] - 'a';
if (!temp[p][t])
{
memset(temp[num], 0, sizeof(temp[num]));
val[num] = 0;
temp[p][t] = num++;
}
p = temp[p][t];
}
val[p] = len;
}
void dfs(int k)
{
int p = 0;
for (int i = k; i < l; i++)
{
int t = h[i] - 'a';
if (!temp[p][t])
{
break;
}
p = temp[p][t];
if (val[p])
{
f[k] = (f[k + val[p]] + f[k]) % 20071027;
}
}
}
void input()
{
int cases = 0;
while (scanf("%s", h) != EOF)
{
int n;
scanf("%d", &n);
memset(f, 0, sizeof(f));
memset(temp[0], 0, sizeof(temp[0]));
num = 1;
for (int i = 0; i < n; i++)
{
scanf("%s", ch);
build(ch);
}
l = strlen(h);
f[l] = 1;
for (int i = l - 1; i >= 0; i--)
{
dfs(i);
}
printf("Case %d: %d\n", ++cases, f[0]);
}
}
int main()
{
input();
return 0;
}


 

这篇关于trie基本用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062391

相关文章

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Python中的sort()和sorted()用法示例解析

《Python中的sort()和sorted()用法示例解析》本文给大家介绍Python中list.sort()和sorted()的使用区别,详细介绍其参数功能及Timsort排序算法特性,涵盖自适应... 目录一、list.sort()参数说明常用内置函数基本用法示例自定义函数示例lambda表达式示例o

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

MySQL ORDER BY 语句常见用法、示例详解

《MySQLORDERBY语句常见用法、示例详解》ORDERBY是结构化查询语言(SQL)中的关键字,隶属于SELECT语句的子句结构,用于对查询结果集按指定列进行排序,本文给大家介绍MySQL... 目录mysql ORDER BY 语句详细说明1.基本语法2.排序方向详解3.多列排序4.常见用法示例5.