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 ...
随机推荐
- nodeJS服务器的创建和重新启动
一: 首先在nodejs项目里创建一个server.js文件,输入下面代码 var http = require("http"); http.createServer(functi ...
- 【C语言】符号优先级
一. 问题的引出 今天看阿里的笔试题,看到一个非常有意思的题目,但是很容易出错. 题目:如下函数,在32bit系统foo(2^31-3)的值是: Int foo(int x) { return x&a ...
- Ansible 使用普通用户远程执行playbook
设置ansible使用普通用户jsxge远程连接执行playbook 1. ansible控制端创建普通用户jsxgecd /homeuseradd jsxgechown -R jsxge.wheel ...
- JS控制音频顺序播放
做一项目,用到“叫号功能”,网页上有一“叫号”按钮,点击后就读数据库中存的号码,如123号, 然后就发声音出来, 思路是网上下载0123456789的叫号声音,然后按钮点击事件里就在JS里写用那个HT ...
- db2编目抽取
最近迁移DB2数据库从AIX到Linux平台:需要整理所有服务器编目,服务器有aix和linux,手动整理肯定较慢切容易出错,下面写脚本解决. 策略: 1.编写脚本提取服务器编目的脚本,编目信息保存到 ...
- 【iCore4 双核心板_ARM】例程十九:USBD_MSC实验——虚拟U盘
实验步骤: 1.将SD卡插在SD卡槽中. 2.将跳线冒跳至USB_OTG,将USB_OTG通过Micor USB线与USB主机(电脑)相连. 3.烧写程序,我的电脑中将出现一个磁盘. 实验现象: 核心 ...
- Java知多少(87)选择框和单选按钮
选择框.单选框和单选按钮都是选择组件,选择组件有两种状态,一种是选中(on),另一种是未选中(off),它们提供一种简单的 “on/off”选择功能,让用户在一组选择项目中作选择. 选择框 选择框(J ...
- 第三百九十三节,Django+Xadmin打造上线标准的在线教育平台—Xadmin后台进阶开发配置
第三百九十三节,Django+Xadmin打造上线标准的在线教育平台—Xadmin后台进阶开发配置 设置后台某个字段的排序规则 在当前APP里的adminx.py文件里的数据表管理器里设置 order ...
- Java8使用@sun.misc.Contended避免伪共享(False Sharing)
伪共享(False Sharing) Java8中用sun.misc.Contended避免伪共享(false sharing) Java8使用@sun.misc.Contended避免伪共享
- Spark学习笔记——读写MySQL
1.使用Spark读取MySQL中某个表中的信息 build.sbt文件 name := "spark-hbase" version := "1.0" scal ...