Bathroom terminal

Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Next to him is tape-player with a hand-written message “Play Me”. He finds a tape in his own back pocket. After putting the tape in the tape-player, he sees a key hanging from a ceiling, chained to some kind of a machine, which is connected to the terminal next to him. After pressing a Play button a rough voice starts playing from the tape:

"Listen up Smith. As you can see, you are in pretty tough situation and in order to escape, you have to solve a puzzle.

You are given N strings which represent words. Each word is of the maximum length L and consists of characters ‘a’-‘e’. You are also given M strings which represent patterns. Pattern is a string of length  ≤  L and consists of characters ‘a’-‘e’ as well as the maximum 3 characters ‘?’. Character ‘?’ is an unknown character, meaning it can be equal to any character ‘a’-‘e’, or even an empty character. For each pattern find the number of words that matches with the given pattern. After solving it and typing the result in the terminal, the key will drop from the ceiling and you may escape. Let the game begin."

Help Smith escape.

Input
The first line of input contains two integers N and M (1 ≤ N ≤  100 000, 1 ≤ M ≤  5000), representing the number of words and patterns respectively.

The next N lines represent each word, and after those N lines, following M lines represent each pattern. Each word and each pattern has a maximum length L (1 ≤ L ≤ 50). Each pattern has no more that three characters ‘?’. All other characters in words and patters are lowercase English letters from ‘a’ to ‘e’.

Output
Output contains M lines and each line consists of one integer, representing the number of words that match the corresponding pattern.

题解:

dfs过程中记得匹配完一个单词标记一下,因为下面的匹配方式可能导致一个单词多次被匹配。

#include<bits/stdc++.h>
using namespace std;
const int M = 55;
const int N = 1e5+7;
int nxt[N*M][5], val[N*M], flag[N*M];
char s[M], str[M];
int n, m, size = 1;
void insert(char s[]){int now = 1;for (int i=0; s[i]; i++){int to=s[i]-'a';if (!nxt[now][to]) nxt[now][to] = ++size;now = nxt[now][to];}val[now]++;
}
int dfs(int loc, int now){if (!now) return 0;if (!str[loc]){if (flag[now] == m+1) return 0;else{flag[now] = m+1;return val[now];}}if (str[loc] != '?') return dfs(loc+1, nxt[now][str[loc]-'a']);int res = dfs(loc+1, now);for (int i=0; i<5; i++)res += dfs(loc+1, nxt[now][i]);return res;
}
int main(){scanf("%d%d", &n, &m);getchar();for (int i=1; i<=n; i++){scanf("%s",s);insert(s);}while (m--){scanf("%s",str);printf("%d\n", dfs(0, 1));}
}

Bathroom terminal——tril树相关推荐

  1. Bubble Cup X - Finals [Online Mirror] G. Bathroom terminal(STL)

    Description Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to pipes. Nex ...

  2. 回归树与基于规则的模型(part2)--简单回归树

    学习笔记,仅供参考,有错必纠 回归树与基于规则的模型 简单回归树 简单回归树将数据划分为若干组,其中组内的样本点在结果变量取值上具有一定的同质性.为了实现这种同质性划分,回归树需要决定: 用于切分的预 ...

  3. vim入门及常用命令

    进入/退出vim 进入: ctrl+alt+t进入终端,输入以下指令: vim vim filename 退出 命令 含义 :q 不保存直接退出 :q! 强制退出 :w 仅保存不退出 :wq 保存并退 ...

  4. AlphaGo的制胜秘诀:蒙特卡洛树搜索初学者指南

    编译 | reason_W 出品 |  AI科技大本营(公众号ID:rgznai100) 长久以来,计算机在围棋领域不可能达到人类专家的水平一直是学术界的主流观点.围棋,被认为是人工智能的" ...

  5. 多元回归树分析Multivariate Regression Trees,MRT

    作者:陈亮 单位:中国科学院微生物研究所 多元回归树分析 多元回归树(Multivariate Regression Trees,MRT)是单元回归树的拓展,是一种对一系列连续型变量递归划分成多个类群 ...

  6. python实现树有多少种方法_教程 | 从头开始:用Python实现决策树算法

    如何计算并评价数据集中地候选分割点(Candidate Split Point) 如何在决策树结构中排分配这些分割点 如何在实际问题中应用这些分类和回归算法 一.概要 本节简要介绍了关于分类及回归树( ...

  7. 基于Tire树和最大概率法的中文分词功能的Java实现

    对于分词系统的实现来说,主要应集中在两方面的考虑上:一是对语料库的组织,二是分词策略的制订. 1.   Tire树 Tire树,即字典树,是通过字串的公共前缀来对字串进行统计.排序及存储的一种树形结构 ...

  8. Trie(前缀树/字典树)及其应用

    from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...

  9. LR(1)分析表-语法树-四元式

    这学期的编译原理终于学完了,不愧是号称最难的科目.要用C++从头到尾实现一下小型编译器,还真不容易啊.不过总算是做完了. 首先上文法,这个文法是根据上一篇博客简化的,但还是有一点问题的,暂时发现有一个 ...

最新文章

  1. CMD——ping及用其检测网络故障
  2. 不少Java程序员都觉得Lambda表达式很鸡肋,它到底有何用呢?
  3. Access denied (java.lang.RuntimePermission getClas
  4. boost::cv_status相关的测试程序
  5. python面向对象语言_Python语言基础之——面向对象编程
  6. Redis线程IO模型的秘密知多少
  7. LeetCode-20:有效的括号
  8. 深入浅出“跨视图粒度计算”--2、INCLUDE表达式
  9. 解除Xcode中Miss File的警告
  10. 大兴服务器硬盘回收行情价格,回收服务器 硬盘 内
  11. CornerStone的使用
  12. Python|加权平均法读取灰度化图像介
  13. 拍牌服务器提交中断,沪牌拍牌的经验总结
  14. 中兴java笔试题_中兴Java 笔试题.doc
  15. C++定义复数类(Complex)
  16. oracle between and 边界查询用法
  17. 会议,如何从智能走向智慧?——四川有生发布智慧会议平台
  18. 公司固定资产管理系统
  19. PO-01,cas:1258323-36-2乙酰丙酮酸二(4-苯基-噻吩[3,2-C]吡啶-C2,N)合铱(III)
  20. 线性代数:Span学习笔记

热门文章

  1. 1. 使用Popup组件自定义弹框提示页面
  2. QDUOJ-数数字-异或运算
  3. 移动硬盘不小心格式化怎么恢复数据呢?
  4. Redash -- Redash问题排查
  5. 小程序电子签名,解决安卓保存图片是黑色背景的问题
  6. Firebase模块的使用方法
  7. .net研究院之爬虫(第三方工具包HtmlAgilityPack)
  8. 图像sensor的特性和驱动解析视频教程-第4/11季-海思-朱有鹏-专题视频课程
  9. 安装 JupyterHub 踩坑指南 —— 如何通过 JupyterHub 实现多用户管理
  10. 电子商务新风口在哪?