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

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然后输入N行字符串,由'?','*'和26个小写字母组成,'?'代表任意一个小写字母,'*'代表0个或者多个小写字母,紧接着输入M行字符串,问每行字符串和前面N行字符串中哪些是等价的。
解题方法:经典的DFS+字典树,先建立建立一颗字典树,然后用DFS进行搜索。
#include <stdio.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std; int ans[];
int nCount;
char str[]; typedef struct node
{
vector <int> id;
node *next[];
node()
{
id.clear();
memset(next, , sizeof(next));
}
~node()
{
id.clear();
}
}TreeNode; int GetIndex(char ch)
{
switch(ch)
{
case '?':
return ;
case '*':
return ;
default:
return ch - 'a';
}
} //建立字典树
void Insert(TreeNode *pRoot, char pstr[], int id)
{
int nLen = strlen(pstr);
TreeNode *p = pRoot;
for (int i = ; i < nLen; i++)
{
int index = GetIndex(pstr[i]);
if (p->next[index] == NULL)
{
p->next[index] = new TreeNode;
}
p = p->next[index];
}
p->id.push_back(id);//每个单词的结尾保存该单词的编号
} void DFS(TreeNode *pRoot, int index)
{
if (pRoot->next[] != NULL)
{
//忽略掉'*',即'*'代表0个字母
DFS(pRoot->next[], index);
}
if (index == strlen(str))
{
//如果遍历到了最后一个字母,则把编号加进去
for (int i = ; i < pRoot->id.size(); i++)
{
ans[nCount++] = pRoot->id[i];
}
return;
}
//如果字典树和字符串当前都是字母,则同时跳过该字母
if (pRoot->next[GetIndex(str[index])] != NULL)
{
DFS(pRoot->next[GetIndex(str[index])], index + );
}
if (pRoot->next[] != NULL)
{
//如果字典树中当前是'?',直接跳过
DFS(pRoot->next[], index + );
}
if (pRoot->next[] != NULL)
{
//如果字典树中当前是‘*’,则让‘*’代表多个字符
for (int i = index; i < strlen(str); i++)
{
DFS(pRoot->next[], i + );
}
}
} void DeleteNode(TreeNode *pRoot)
{
if (pRoot != NULL)
{
for (int i = ; i < ; i++)
{
DeleteNode(pRoot->next[i]);
}
}
delete pRoot;
} int main()
{
int N, M, nID = ;
scanf("%d%d", &N, &M);
TreeNode *pRoot = new TreeNode;
for (int i = ; i < N; i++)
{
scanf("%s", str);
Insert(pRoot, str, nID++);
}
for (int i = ; i < M; i++)
{
nCount = ;
scanf("%s", str);
DFS(pRoot, );
if (nCount == )
{
printf("Not match\n");
}
else
{
sort(ans, ans + nCount);
printf("%d", ans[]);
for (int j = ; j < nCount; j++)
{
if (ans[j - ] != ans[j])
{
printf(" %d", ans[j]);
}
}
printf("\n");
}
}
DeleteNode(pRoot);
return ;
}

POJ 1816 Wild Words的更多相关文章

  1. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  2. 【POJ】1816 Wild Words

    DFS+字典树.题目数据很BT.注意控制DFS深度小于等于len.当'\0'时,还需判断末尾*.另外,当遇到*时,注意讨论情况. #include <iostream> #include ...

  3. poj 1816 (Trie + dfs)

    题目链接:http://poj.org/problem?id=1816 思路:建好一颗Trie树,由于给定的模式串可能会重复,在原来定义的结构体中需要增加一个vector用来记录那些以该节点为结尾的字 ...

  4. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  5. [转] POJ字符串分类

    POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...

  6. E - Petya and Exam CodeForces - 832B 字典树+搜索

    E - Petya and Exam CodeForces - 832B 这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍. 代码还是很好理解的 ...

  7. 字典树trie的学习与练习题

    博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...

  8. 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 ...

  9. HDU 1816, POJ 2723 Get Luffy Out(2-sat)

    HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...

随机推荐

  1. sqlserver 链接 ODBC 访问 MySql

    环境:windows 2008 + sqlserver 2008 一 安装 mysql-connector-odbc-5.2.5-winx64.msi 必须安装5.2.5,安装mysql-connec ...

  2. 通过Anuglar Material串串学客户端开发 - javascript编译和gulpfile.js

    Angular Material不仅仅有本身框架的源代码,还有在这个框架上实现的一个应用docs.更为强大的是,这个应用是真正的产品网站:就是它的官网.我有理由相信,这个网站是从源代码直接发布的,从网 ...

  3. [stm32] LED

    /**************************************************************************** * 文件名: main.c * 内容简述: ...

  4. Mac上搭建直播服务器Nginx+rtmp

    简介 nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择,本人在网上整理了安装流程,分享给大家并且作备忘. 步骤安装 1.安装Homebrow Homebrew简 ...

  5. JS 日期格式化和解析工具

    本来想模仿Java里面的SimpleDateFormat()对象的,但是感觉这样用起来不方便,所以还是直接写成单独的方法算了. 原文链接 日期格式化 使用说明 formatDate(date, fmt ...

  6. Windows Server 2008R2 配置网络负载平衡(NLB)

    目录 配置环境 安装 安装网络负载平衡 安装Web服务器 IIS 配置 测试 其它 配置环境 VMware:(版本10.0.01) 主集群IP:192.168.220.102 VM1:192.168. ...

  7. atitit. 浏览器插件 控件 applet 的部署,签名总结 浏览器 插件 控件 的签名安全机制o9o

    atitit. 浏览器插件 控件   applet 的部署,签名总结 浏览器 插件 控件 的签名安全机制o9o 1. 服务器部署签名 1 2. 签名流程::生成密钥..导出cert正书,签名 1 3. ...

  8. Atitit.guice3 ioc 最佳实践 o9o

    Atitit.guice3 ioc  最佳实践 o9o 1. Guice的优点and跟个spring的比较 1 2. 两个部分:::绑定and注入@Inject 1 3. 绑定所有的方法总结 2 3. ...

  9. Essential C++中文版——满汉全席之外

    满汉全席之外 Stanley B. Lippman 所著的C++ Primer 雄踞书坛历久不衰,堪称C++最佳教科书.但是走过十个年头之后,继1237 页的C++ Primer 第3 版,Lippm ...

  10. Spring之事件发布系统

    springboot应用,启动spring容器大致有如下几个过程: 容器开始启动 初始化环境变量 初始化上下文 加载上下文 完成 对应的Spring应用的启动器的监听器可以监听以上的过程,接口如下: ...