题目链接:

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]的更多相关文章

  1. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  2. POJ 1816 Wild Words

    Wild Words Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4412   Accepted: 1149 Descri ...

  3. poj 1204 Word Puzzles(字典树)

    题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...

  4. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  5. POJ 1002 487-3279(字典树/map映射)

    487-3279 Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 309257        Accepted: 5 ...

  6. POJ 2408 - Anagram Groups - [字典树]

    题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...

  7. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  8. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  9. HDU1298 字典树+dfs

    T9 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

随机推荐

  1. swift3 单例写法

    import UIKit class SingleOnce { // 单例 static let shared = SingleOnce.init() private init(){} // 其他方法 ...

  2. httpclient检查某个链接是否可用

    private boolean checkUrlIsValid(String url) { CloseableHttpClient httpClient = HttpClients.createDef ...

  3. C#获取起始位置以及添加全局资源字典

    获取起始位置 Path.Combine(AppDomain.CurrentDomain.BaseDirectory); 添加全局资源 string temp = "this is a str ...

  4. Java知多少(42)泛型通配符和类型参数的范围

    本节先讲解如何限制类型参数的范围,再讲解通配符(?). 类型参数的范围 在泛型中,如果不对类型参数加以限制,它就可以接受任意的数据类型,只要它是被定义过的.但是,很多时候我们只需要一部分数据类型就够了 ...

  5. SAP项目的整体预算大概是多少?

    例如一家规模包括(5-6个生产型子公司),员工5000人,市值100亿,销售额每年20亿左右的集团公司.上SAP,预算怎么算? 这其中:1)软件费用一般多少? 2)用户费用是怎么算的? 3)维护费用又 ...

  6. WebSphere集群环境修改IHS端口号的方法 分类: WebSphere 2015-08-06 13:41 14人阅读 评论(0) 收藏

    参考资料:http://wenku.baidu.com/link?url=E9BkuEjJ16i9lg7l91L0-xhKCYkHV0mAnlwAeSlDCFM4TjZyk4ZVxmUu64BGd4F ...

  7. Kubernetes集群部署之三ETCD集群部署

    kuberntes 系统使用 etcd 存储所有数据,本文档介绍部署一个三节点高可用 etcd 集群的步骤,这三个节点复用 kubernetes 集群机器k8s-master.k8s-node-1.k ...

  8. C# Winform 防止MDI子窗体重复打开

    可以在MDI主窗体中添加以下方法. //防止打开多个窗体 private bool ShowChildrenForm(string p_ChildrenFormText) { int i; //依次检 ...

  9. ubuntu 创建swap分区

    通常,Linux系统中swap分区可以通过两种方式指定,分别为:一.在磁盘分区的时候格式化一个swap分区:二.在文件系统中创建一个swap文件作为swap分区.此文主要介绍第二种方式.步骤如下: 1 ...

  10. [TensorBoard] *Cookbook - Tensorboard

    Ref: https://www.tensorflow.org/get_started/summaries_and_tensorboard 可视化对于Training的重要性,不言而喻. 代码示范 # ...