当前位置: 首页 > news >正文

【Trie】 UVA1401 Remember the Word - 教程

代码

#include <string.h>#include <stdlib.h>#include <algorithm>#include <vector>#include <functional>#include <math.h>#include <string>#include <iostream>using namespace std;typedef long long ll;typedef pair<int, int> pii;#define _for(i, a, b) for (int i = (a); i < (b); i++)#define _rep(i, a, b) for (int i = (a); i <= (b); i++)#define fio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)#define reg register#define ri reg intnamespace faio{char buf[1 << 20], *p1, *p2;#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF: *p1++)template <typename T> inline void read(T &t) {ri v = getchar();T f = 1;t = 0;while (!isdigit(v)) {if (v == '-')f = -1;v = getchar();}while (isdigit(v)) {t = (t << 3) + (t << 1) + v - '0';v = getchar();}t *= f;}template <typename T,typename... Args> inline void read(T &t, Args&... args) {read(t);read(args...);}}constexpr int N = 300010, mod = 20071027;char str[N], word[128];int dp[N];typedef struct Trie_t{bool is_end;Trie_t* son[26];}*Trie;Trie NewNode() {Trie ret = (Trie) malloc(sizeof(Trie_t));for (int i = 0; i < 26; i++) ret->son[i] = NULL;ret->is_end = false;return ret;}void FreeNode(Trie p) { // 释放 Trie 树if (p == NULL) return ;for (int i = 0; i < 26; i++) FreeNode(p->son[i]);free(p);}void insert(Trie root, const char* _str){Trie u = root; int len = strlen(_str);for (int i = 0; i < len; i++) {int idx = _str[i] - 'a';if (u == NULL) printf("u is NULL");if (u->son[idx] == NULL) u->son[idx] = NewNode();u = u->son[idx];}u->is_end = true;}void solve(Trie root){static int Kase = 0;int n = strlen(str); dp[n] = 1;for (int i = n - 1; i >= 0; i--) {// 在 Trie 树里面找Trie u = root;for (int j = i; j < n; j++) {int idx = str[j] - 'a';if (u->son[idx] == NULL) break;u = u->son[idx];if (u->is_end) dp[i] = (dp[i] + dp[j + 1] + mod) % mod;}}printf("Case %d: %d\n", ++Kase, dp[0]);}int main(){while (scanf("%s", str) == 1) {int p; scanf("%d", &p);Trie root = NewNode();memset(dp, 0, sizeof(dp));while (p--) {scanf("%s", word);insert(root, word);}solve(root);FreeNode(root);}return 0;}
http://www.zskr.cn/news/19705.html

相关文章:

  • 2025 年 MES 服务商 TOP 平台机构推荐排行榜,mes 系统 /mes 软件 /mes 制造执行系统 /mes 生产制造执行系统 /mes 生产管理系统公司推荐
  • 2025 年10月 WMS 服务商最新推荐榜单,wms系统 wms软件,wms仓库管理软件,wms仓库管理系统软件公司推荐
  • Matlab凭借GUI实现点云的中值滤波(附最简版)
  • CF数据结构题做题记录-1
  • CCPC2023女生专场 游记(VP)
  • tp3.2不再生成Runtime/Logs日志
  • 心得:刷算法的痛点-只根据题目的case思考,不考虑边界情况,写出一坨shit
  • 2.4 DQN 变体(Rainbow)
  • Emacs折腾日记(三十二)——org mode的基本美化
  • pp
  • vim配置使用
  • shell展开shell数组
  • 原木
  • 2025年10月镀锌卷板厂家最新推荐排行榜,有花镀锌卷板,无花镀锌卷板,高锌层镀锌卷板,批发镀锌卷板公司推荐
  • 会话跟踪方案
  • 阻塞、非阻塞、同步、异步的区别是什么?
  • Linux文本编辑三剑客之grep
  • Linux文本编辑三剑客之awk
  • 详细介绍:【ROS2学习笔记】节点篇:节点概述
  • 2025.10.12 - 20243867孙堃2405
  • Linux查看一个文件的时候发生了什么?
  • 八、系统软件
  • 七、输入输出和存储系统
  • 那快把题端上来吧(五)
  • 构建易受攻击的AWS DevOps环境:CloudGoat攻防实战
  • MySQL 5.7版本,搭建一个两主一从的多源主从复制环境
  • 决胜职场:以准备为刃,破局而出
  • 计网思维导图总结
  • 如何下载不同版本的像素地牢
  • DropLoRA 论文浅读:通过动态子空间学习突破 LoRA 的性能瓶颈