POJ 1816 - Wild Words - [字典树+DFS]
题目链接:
http://poj.org/problem?id=1816
http://bailian.openjudge.cn/practice/1816?lang=en_US
Time Limit: 2000MS Memory Limit: 65536K
Description
A word is a string of lowercases. A word pattern is a string of lowercases, '?'s and '*'s. In a pattern, a '?' matches any single lowercase, and a '*' matches none or more lowercases.
There are many word patterns and some words in your hand. For each word, your task is to tell which patterns match it.
Input
The first line of input contains two integers N (0 < N <= 100000) and M (0 < M <=100), representing the number of word patterns and the number of words. Each of the following N lines contains a word pattern, assuming all the patterns are numbered from 0 to N-1. After those, each of the last M lines contains a word.
You can assume that the length of patterns will not exceed 6, and the length of words will not exceed 20.
Output
For each word, print a line contains the numbers of matched patterns by increasing order. Each number is followed by a single blank. If there is no pattern that can match the word, print "Not match".
Sample Input
5 4
t*
?h*s
??e*
*s
?*e
this
the
an
is
Sample Output
0 1 3
0 2 4
Not match
3
题意:
给出 $n$ 个模式串(只包含小写字母,"?" 和 "*"),$m$ 个字符串(只包含小写字母),
模式串中 "?" 代表能匹配任意一个字母,"*" 代表能匹配任意多个字母(可以是 $0$ 个)。
现在对于 $m$ 个字符串,查询编号为 $0 \sim n-1$ 这 $n$ 个模式串中有多少个是可以匹配的上的。
题解:
对 $n$ 个模式串建立字典树;对于 $m$ 个字符串的查询,每个字符串都在字典树上进行DFS匹配。
放几组数据:
3 1
t
t*
t**
t
1 1
*j*j
jjj
2 1
abc
abc
abc
AC代码:
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=+;
int n,m; namespace Trie
{
const int SIZE=maxn;
int sz;
struct TrieNode{
vector<int> pos;
int nxt[];
}trie[SIZE];
void init(){sz=;}
int idx(const char& c)
{
if(c=='?') return ;
if(c=='*') return ;
return c-'a';
}
void insert(const string& s,int id)
{
int p=;
for(int i=;i<s.size();i++)
{
int ch=idx(s[i]);
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
trie[p].pos.push_back(id);
} void dfs(vector<int>& ans,int p,const string& s,int k)
{
if(trie[p].nxt[]) {
for(int i=;k+i<=s.size();i++) dfs(ans,trie[p].nxt[],s,k+i);
}
if(k>=s.size())
{
for(int i=;i<trie[p].pos.size();i++) ans.push_back(trie[p].pos[i]);
return;
}
int ch=idx(s[k]);
if(trie[p].nxt[]) dfs(ans,trie[p].nxt[],s,k+);
if(trie[p].nxt[ch]) dfs(ans,trie[p].nxt[ch],s,k+);
}
}; int main()
{
scanf("%d%d",&n,&m);
Trie::init();
char s[];
for(int i=;i<n;i++)
{
scanf("%s",&s);
Trie::insert(s,i);
} vector<int> ans;
for(int i=;i<=m;i++)
{
scanf("%s",&s);
ans.clear();
Trie::dfs(ans,,s,);
if(ans.empty()) printf("Not match\n");
else
{
sort(ans.begin(),ans.end());
ans.erase(unique(ans.begin(),ans.end()),ans.end());
for(int k=;k<ans.size();k++) {
printf("%d%c",ans[k],(k==ans.size()-)?'\n':' ');
}
}
}
}
POJ 1816 - Wild Words - [字典树+DFS]的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- POJ 1816 Wild Words
Wild Words Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4412 Accepted: 1149 Descri ...
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- POJ 1002 487-3279(字典树/map映射)
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 309257 Accepted: 5 ...
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序
题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...
- HDU1298 字典树+dfs
T9 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
随机推荐
- Asp.Net MVC4中的全局过滤器
可以对整个项目进行全局监控. 新建一个MVC4项目,可以在global.asax文件中看到如下代码: FilterConfig.RegisterGlobalFilters(GlobalFilters ...
- Kubernetes的ConfigMap解析
ConfigMap功能在Kubernetes1.2版本的时候就有了,许多应用程序会从配置文件.命令行参数或环境变量中读取配置信息.这些配置信息需要与docker image解耦,你总不能每修改一个配置 ...
- Roller5.0.3安装配置部署 step by step
一.下载roller 下载地址:http://roller.apache.org/downloads/downloads.html下载下来之后,解压包含两部份doc.webapps 二.准备环境 1. ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- MXNET:深度学习计算-模型构建
进入更深的层次:模型构造.参数访问.自定义层和使用 GPU. 模型构建 在多层感知机的实现中,我们首先构造 Sequential 实例,然后依次添加两个全连接层.其中第一层的输出大小为 256,即隐藏 ...
- Go Revel - Jobs(任务调度模块)
revel提供了一个框架用于脱离请求流程的执行异步任务,一般用来执行经常运行的任务.更新缓存数据或发送邮件等. ##启用 该框架作为一个可选的revel模块,默认并不启用.需要更改应用配置来启用它: ...
- 【转】详解在visual studio中使用git版本系统(图文)
http://blog.csdn.net/wojilu/article/details/6976230 很多人已经在使用git(或正在转移到git上),在github.com上,也看到不少国内同学的开 ...
- Go语言实践_实现一(客户端)对一(服务器端)聊天室
一.目的 使用Go语言实现一个服务器端与客户端的聊天室. 软件:Goland,Go1.9 代码仓库地址 二.思路 1,首先启动服务器端,使用listen_socket函数监听IP地址上的客户端连接: ...
- 用Go语言实现一个简单的聊天机器人
一.介绍 目的:使用Go语言写一个简单的聊天机器人,复习整合Go语言的语法和基础知识. 软件环境:Go1.9,Goland 2018.1.5. 二.回顾 Go语言基本构成要素:标识符.关键字.字面量. ...
- laravel5.4中验证与错误提示设置
1.对于交互提交数据,验证如: #验证 $this->validate(\request(),[ 'title' => 'required|string|min:3|max:20', 'c ...