题目链接:

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. iOS蓝色和黄色文件夹新建方式区别(区别之前)

    关于这个问题,好像xcode9之前还是多少来着,创建方式是这样: 1.New Group创建的黄色文件夹是“假”文件夹,也就是说 show in finder 是看不到的 解释:这个文件夹并不是真正的 ...

  2. git合并多个提交

    git合并多个提交 [时间:2016-11] [状态:Open] [关键词:git,git rebase,合并提交,commit] 0. 引言 本文是关于Git提交记录修改的方法,主要是将多个提交记录 ...

  3. Java知多少(72)文件的随机读写

    Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问.使用这个类,可以跳转到文件的任意位置读写数据.程序可以在随机文件中插入数据,而不会破坏该文件的其他数据.此外,程 ...

  4. Java如何根据主机名(域名)获取IP地址?

    在Java编程中,如何根据主机名(域名)获取IP地址? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将主机名更改为指定的IP地址. pac ...

  5. Git -- 自定义git样式

    在安装Git一节中,我们已经配置了user.name和user.email,实际上,Git还有很多可配置项. 比如,让Git显示颜色,会让命令输出看起来更醒目: $ git config --glob ...

  6. laravel 5.4 中使用migrate

    1. 创建表结构 a. 命令: php artisan make:migration create_posts_table 2.生产文件 <?php use Illuminate\Support ...

  7. swoole web服务

    web.php <?php $http = ); $http->on('request', function ($request, $response) { var_dump($reque ...

  8. MySQL -- 行转列 -- GROUP_CONCAT -- MAX(CASE WHEN THEN)

    列转行:利用max(case when then) SELECT `name`, MAX( CASE WHEN course='语文' THEN score END ) AS 语文, MAX( CAS ...

  9. ASP.NET MVC 4 (八) URL链接和Ajax帮助函数

    使用帮助函数创建链接 MVC提供一些帮助函数创建链接,这些函数根据路径映射表自动调整生成的URL: 说明 示例 输出结果 应用程序相对URL Url.Content("~/Content/S ...

  10. nginx-启动|关闭|重新加载配置文件的命令

    1.1 进入操作目录 D: cd D:\NginxTest\nginx-1.10.2 1.2 启动指令 nginx -c conf\nginx.conf 1.3 关闭指令 nginx -s stop ...