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

相关文章

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Java8 Collectors.toMap() 的两种用法

《Java8Collectors.toMap()的两种用法》Collectors.toMap():JDK8中提供,用于将Stream流转换为Map,本文给大家介绍Java8Collector... 目录一、简单介绍用法1:根据某一属性,对对象的实例或属性做映射用法2:根据某一属性,对对象集合进行去重二、Du

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent

Java 中 Optional 的用法及最佳实践

《Java中Optional的用法及最佳实践》在Java开发中,空指针异常(NullPointerException)是开发者最常遇到的问题之一,本篇文章将详细讲解Optional的用法、常用方... 目录前言1. 什么是 Optional?主要特性:2. Optional 的基本用法2.1 创建 Opti

Kotlin 协程之Channel的概念和基本使用详解

《Kotlin协程之Channel的概念和基本使用详解》文章介绍协程在复杂场景中使用Channel进行数据传递与控制,涵盖创建参数、缓冲策略、操作方式及异常处理,适用于持续数据流、多协程协作等,需注... 目录前言launch / async 适合的场景Channel 的概念和基本使用概念Channel 的

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返