Wild Words
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5567   Accepted: 1475

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

Source

大致题意:给n个模式串和m个字符串,*可以代表任意字符串(空串),?只能代表一个特定字符,问每个字符串有哪些模式串与它匹配?
分析:题目给了很多模式串,将它们放在trie树上.因为可能会有相同的模式串,所以需要记录一下当前点为多少个模式串的终点,利用vector或者邻接表都可以.匹配的话利用dfs,一个指针在trie上走,另一个在字符串上走,每次匹配有3种可能:1.直接走当前字符的下一位. 2.走?的一位. 3.走*的一位.对于?和*,用特殊的数字26,27存在trie中.*的转移比较麻烦,它有可能只占一个字符,有可能是空串,也有可能占很多串,这样就要分三种情况搜下去.对于第三种情况,由于trie存的是边,并不知道当前点是不是由*转移过来的,必须要在trie树中表示每个点的字符.需要注意的是搜索的终点并不是字符串的完结,而是trie跳到了终点,因为可能模式串末尾有一大堆**这么一说非常抽象,看代码可能更容易理解一些.
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm> const int maxn = ; using namespace std;
int n, m, len, tot = , head[maxn], nextt[maxn], to[maxn], tott = ;
char s[maxn], s2[maxn];
bool flag[maxn], can; vector <int> E[maxn]; struct node
{
char ch;
int tr[];
void clear(char c)
{
ch = c;
memset(tr, , sizeof(tr));
}
}e[maxn * ]; int zhuanhuan(char x)
{
if (x == '?')
return ;
if (x == '*')
return ;
else
return x - 'a';
} void add(int x, int y)
{
to[tott] = y;
nextt[tott] = head[x];
head[x] = tott++;
} void insert(char *ss, int id)
{
int u = ;
len = strlen(ss + );
for (int i = ; i <= len; i++)
{
int t = zhuanhuan(ss[i]);
if (!e[u].tr[t])
{
e[u].tr[t] = ++tot;
e[tot].clear(ss[i]);
}
u = e[u].tr[t];
}
add(u, id); //邻接表
} void dfs(int dep, int u)
{
if (dep == len + )
{
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
can = flag[v] = ;
}
if (e[u].tr[]) //trie没走完
dfs(dep, e[u].tr[]);
return;
}
int temp = zhuanhuan(s2[dep]);
if (e[u].tr[temp])
dfs(dep + , e[u].tr[temp]);
if (e[u].tr[])
dfs(dep + , e[u].tr[]);
if (e[u].tr[])
{
dfs(dep + , e[u].tr[]);
dfs(dep, e[u].tr[]);
}
if (e[u].ch == '*') //如果当前点是*
dfs(dep + , u);
} int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
scanf("%s", s + );
insert(s, i - );
}
while (m--)
{
can = false;
scanf("%s", s2 + );
len = strlen(s2 + );
memset(flag, false, sizeof(flag));
dfs(, );
if (!can)
{
printf("Not match\n");
continue;
}
for (int i = ; i < n; i++)
if (flag[i])
printf("%d ", i);
printf("\n");
} return ;
}

poj1816 Wild Words的更多相关文章

  1. POJ1816:Wild Words——题解

    http://poj.org/problem?id=1816 比较麻烦的trie. 首先你需要选择针对n还是m建立trie,这里我选择了针对n. 那么就需要面临卡空间的问题. 这里提供了一种链式前向星 ...

  2. 破壳漏洞利用payload—shellshock in the wild

    FireEye关于破壳漏洞(shellshock)在现实中的利用有一篇文章: shellshock in the wild 原文较长,进行了对CGI利用的详细分析,笔者比较感兴趣的是Shellshoc ...

  3. POJ 1816 Wild Words

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

  4. 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。

    [SIGGRAPH 2015][巫师3 狂猎 The Witcher 3: Wild Hunt ]顶级的开放世界游戏的实现技术 作者:西川善司 日文链接  http://www.4gamer.net/ ...

  5. Wild Words

    poj1816:http://poj.org/problem?id=1816 题意:给你n个模板串,然后每个串除了字母,还有?或者*,?可以代替任何非空单个字符,*可以替代任何长度任何串,包括空字符串 ...

  6. 什么是野指针?(What is a wild pointer?)

    未被初始化的变量称为野指针(wild pointer).顾名思义,我们不知道这个指针指向内存中的什么地址,使用不当程序会产生各种各样的问题. 理解下面的例子: int main() { int *p; ...

  7. POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

    题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...

  8. poj 3340 Barbara Bennett's Wild Numbers(数位DP)

    Barbara Bennett's Wild Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3153   A ...

  9. 论文速读(Jiaming Liu——【2019】Detecting Text in the Wild with Deep Character Embedding Network )

    Jiaming Liu--[2019]Detecting Text in the Wild with Deep Character Embedding Network 论文 Jiaming Liu-- ...

随机推荐

  1. mui搜索框 搜索点击事件

    <div class="mui-input-row mui-search"> <input type="search" class=" ...

  2. 在GPT格式的硬盘上,使用EFI启动的方式,安装Win7 64位系统

    Win7 sp1 原装系统,用UltraISO(软碟通) 把U 盘制成Win7 安装的启动U盘 将bootmgfw.efi和shell.efi 加到已制好启动U盘的根目录,并在efi/boot/路径下 ...

  3. 日本IT行业劳动力缺口达22万 在日中国留学生迎来就业好时机 2017/07/18 11:25:09

    作者:倪亚敏 来源:日本新华侨报 发布时间:2017/07/18 11:25:09     据日本政府提供的数据,日本2018年应届毕业生的“求人倍率”已经达到了1.78倍.换言之,就是100名大学生 ...

  4. log4j 配置使用

    使用log4j来管理日志信息,非常方便,下面简单介绍一下整个使用流程: 1.创建简单java项目 2.在类路径下新建log4j.properties文件 3.配置log4j.properties文件 ...

  5. php memcache 使用学习

    Memcache是什么Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力.它可以应对任 ...

  6. Minimum Sum of Array(map迭代器)

    You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...

  7. 学习调用第三方的WebService服务

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的应用中显示,下面就以查询国内手机号码归属地为例进行说明. 首先安利一 ...

  8. HDU 5465 Clarke and puzzle Nim游戏+二维树状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle  Accepts: 42  Submissions: 26 ...

  9. alpha8/10

    队名:Boy Next Door 燃尽图 晗(组长) 今日完成 和队友讨论alpha版的最终界面. 明日工作 确定alpha版既定功能的正常使用. 还剩下哪些任务 账号绑定功能以及账单信息的下载. 困 ...

  10. L1正则化与L2正则化的理解

    1. 为什么要使用正则化   我们先回顾一下房价预测的例子.以下是使用多项式回归来拟合房价预测的数据:   可以看出,左图拟合较为合适,而右图过拟合.如果想要解决右图中的过拟合问题,需要能够使得 $ ...