题目链接:

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. Adobe Photoshop CC2019中文破解版

    今天突发兴致玩PS, 之前安装的是CS6, 下载安装色相环插件后 先采用拷贝文件夹的方式将Coolorus放到plug-in下, 重启发现窗口>扩展程序 这项是灰色的. 于是采用安装coolor ...

  2. 启用phpstorm代码提示功能

    参考:http://www.dawnfly.cn/article-1-331.html mac下实际上将省电禁用即可

  3. SQL使用技巧

    SQLServer 数据库变成单个用户后无法访问问题的解决方法 USE master; GO DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@S ...

  4. 【Windows】Windows中解析DOS的for命令使用

    目录结构: contents structure [+] 简介 for /d ... in ... 案例 案例:打印C://根目录下所有的文件夹名称 案例:打印当前路径下,只有1-3个字母的文件夹名 ...

  5. JSP 性能优化

    无论当前 JavaScript 代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JavaScript 执行过程耗时越久,浏览器等待响应用户输入的时间就越长.浏览器在下载和执行 ...

  6. [Aaronyang] 写给自己的WPF4.5 笔记16[多线程]

      =============潇洒的版权线==========www.ayjs.net===== Aaronyang ========= AY =========== 安徽 六安 杨洋 ======= ...

  7. webpack 使用别名(resolve.alias)解决scss @import相对路径导致的问题

    webpack.conf.js 中 resolve.alias 配置 resolve: { extensions: ['.js', '.vue'], alias: { '@': path.resolv ...

  8. 菜鸟学SSH(十九)——提高用户体验之404处理

    只要做过WEB开发人对于“404”已经再熟悉不过了吧.当我们访问的资源不存在时,它就会跑出来跟你打招呼啦.但是默认情况下,404页面比较简陋,不是很友好.而且一般用户不知道404是个神马东东,还以为是 ...

  9. ajax的工作原理2

    Ajax是异步javascript和xml,可以在不重新加载整个网页的情况下,与服务器异步数据交换,对网页中某个部分进行局部刷新. Ajax是如何实现局部刷新的: 在脚本设置window对象的loca ...

  10. PHP-X介绍

      为什么要开发PHP扩展 PHP-X是用来开发PHP扩展的库.PHP代码写得好好的,为啥要开发PHP扩展呢? 1.我们知道PHP不擅长CPU密集型的操作,那么把CPU密集型的相关代码迁移到扩展上,就 ...