codeforces852GBathroom terminal解题报告
来源:互联网 发布:电子书包软件下载 编辑:程序博客网 时间:2024/06/05 19:18
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 lengthL and consists of characters 'a'-'e'. You are also givenM 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.
The first line of input contains two integers N andM (1 ≤ N ≤ 100 000,1 ≤ M ≤ 5000), representing the number of words and patterns respectively.
The next N lines represent each word, and after thoseN lines, following M lines represent each pattern. Each word and each pattern has a maximum lengthL (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 contains M lines and each line consists of one integer, representing the number of words that match the corresponding pattern.
3 1abcaecaca?c
3
If we switch '?' with 'b', 'e' and with empty character, we get 'abc', 'aec' and 'ac' respectively.
题解:
trie树 查询直接在trie树上查询 查到节点打上tag
查到合法的叶子节点就+上单词个数
代码:
#include <iostream>#include <cstdio>#include <string>using namespace std;const int maxn=5000000+10;int ch[maxn][6];int val[maxn];int tag[maxn];string s;int sz=0;int le;int no;inline int idx(char c){return c-'a';}inline void insert(){int now=0;for(int i=0;i<le;i++){int u=idx(s[i]);if(!ch[now][u]){ch[now][u]=++sz;now=sz;}else now=ch[now][u];}val[now]++;}int cnt=0;inline void find(int now,int pl){if(pl==le){if(val[now]&&tag[now]!=no){cnt+=val[now];tag[now]=no;}return ;}if(s[pl]!='?'){int u=idx(s[pl]);if(ch[now][u])find(ch[now][u],pl+1);}else {find(now,pl+1);for(int i=0;i<5;i++){if(ch[now][i])find(ch[now][i],pl+1);}}}int main(){//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);int n,m;scanf("%d %d",&n,&m);for(int i=1;i<=n;i++){cin>>s;le=s.length();insert();}for(int i=1;i<=m;i++){cin>>s;no=i;cnt=0;le=s.length();find(0,0);printf("%d\n",cnt);}return 0;}
- codeforces852GBathroom terminal解题报告
- UVALive 5861 Hidden Terminal Problem 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- Antiprime解题报告
- expr解题报告
- 华容道解题报告
- tju解题报告
- zju1062/pku1095解题报告
- UsacoGate解题报告 --- 序曲
- ZJU 2060 解题报告
- ZJU 1331 解题报告
- ZJU 1115 解题报告
- APP内可拖动的悬浮按钮
- 机器学习第一章:基本概念
- C++ Windows API 实现多线程
- oradebug--记录
- [book]《科技想要什么》
- codeforces852GBathroom terminal解题报告
- [iOS学习]搜索框(UISearchController)最近踩过的坑
- Vue-Router2.X多种路由实现方式
- 数据库并发操作带来的问题以及解决方案
- mybatis一对一的三种实现方式 数据准备: 使用mysql数据库作为测试。建表语句及测试数据: CREATE TABLE `classes` ( `class_id` int(11) NOT
- mybatis的jdbcType和javaType、oracle,MySQL的对应类型
- 映射规则4
- ios返回指定页
- sqlplus登录,报错,ORA-12154: TNS:could not resolve the connect identifier specified