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. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  2. 常用monkey_app稳定性

    Monkey稳定性测试 1       前言 为方便快速上手Monkey测试相关问题,针对测试中发现的Monkey问题进行了整理总结,供定位Monkey参考. 2       关于Monkey测试 2 ...

  3. 设计模式C++实现

    准备写一系列笔记用来记录学习设计模式的过程,同时写出自己对几种主要的设计模式的理解,以及编码实现,同时总结. 主要参考书籍就是 <Head First Design Patterns>这本 ...

  4. python购物车优化

    一.需求分析 拥有用户接口和商家接口 用户能够进行消费记录查询,充值,购物等功能,消费记录存储于数据库 商家可以进行商品的增删改等操作 二.程序流程图 程序大致流程图如下: 三.代码实现 本程序分成两 ...

  5. JAVA学习笔记--初识容器类库

    一.前言 JAVA中一切皆为对象,因而,持有对象显得尤为重要. 在JAVA中,我们可以通过创建一个对象的引用的方式来持有对象: HoldingObject holding; 也可以创建一个对象数组来持 ...

  6. Unity学习笔记草稿篇(一)为unity配置添加VS智能感知

    1. 打开要编辑的配置文件: 2. 菜单栏 -> xml -> 架构(schema) -> 添加或使用xsd.如下图所示:

  7. MobSF 框架安装使用部署

    1.MobSF 简介 MobSF是Mobile Security Framework的缩写,这是一款智能化的开源移动应用(Android.IOS.Windows)测试框架,可以对应用进行动态.静态分析 ...

  8. ES6的新特性(21)——Proxy

    Proxy 概述 Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种“元编程”(meta programming),即对编程语言进行编程. Proxy 可以理解成,在目标对 ...

  9. Java:有关自定数组的学习

    Java:有关==自定数组==的学习 在 ==<Java程序设计与数据结构教程>== 里我在==P212~P213==页看到一个GradeRange的程序,它用的数组是自定设定的Grade ...

  10. 第四周 实验一 Java开发环境的熟悉 报告

    Java开发环境的熟悉 实验内容 1.IDEA的安装过程 2.使用IDEA代替虚拟机运行.编译.调试Java程序 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)&g ...