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. [计算机图形学] 基于C#窗口的Bresenham直线扫描算法、种子填充法、扫描线填充法模拟软件设计(二)

    上一节链接:http://www.cnblogs.com/zjutlitao/p/4116783.html 前言: 在上一节中我们已经大致介绍了该软件的是什么.可以干什么以及界面的大致样子.此外还详细 ...

  2. 前端框架layui

    可以了解下jQuery组件layer layui开始使用Layui兼容除IE6/7以外的全部浏览器,并且绝大多数结构支持响应式 弹出层如果你使用的是Layui,那么你直接在官网下载layui框架即可, ...

  3. FreeCodeCamp 前端初级算法(个人向)

    freecodecamp 初级算法地址戳这里 Reverse a String 翻转字符串 function reverseString(str) { str=str.split("&quo ...

  4. spring源码 — 一、IoC容器初始化

    IoC容器初始化 注意:本次的spring源码是基于3.1.1.release版本 容器:具有获取Bean功能--这是最基本功能,也是BeanFactory接口定义的主要行为,在添加了对于资源的支持之 ...

  5. sqlserver数据库安全函数、配置函数、游标函数、行级函数、排名函数、元数据函数、系统统计函数 、文本和图像函数--收藏着有用

    行级函数:下列行集函数将返回一个可用于代替 Transact-SQL 语句中表引用的对象. CONTAINSTABLE 返回具有零行.一行或多行的表,这些行的列中包含的基于字符类型的数据是单个词语和短 ...

  6. 493萬Gmail用戶的賬號密碼遭洩露,Google否認自己存在安全漏洞

    最近,大公司在互聯網信息安全問題上狀況頻出.上週,蘋果因iCloud被黑客攻擊而導致大量明星私照外洩,著實是熱鬧了一陣.而Google也來湊熱鬧了.據俄羅斯媒體CNews消息,近493萬Gmail用戶 ...

  7. Linux环境安装jdk

    Linux中JDK1.6的安装和配置方法 一.安装 创建安装目录,在/usr/java下建立安装路径,并将文件考到该路径下: # mkdir /usr/java 1.jdk-6u11-linux-i5 ...

  8. zz Windows 10安装教程:硬盘安装Win10 系统步骤(适合32位和64位)

    Windows 10安装教程:硬盘安装Win10 系统步骤(适合32位和64位) Posted on 2015年01月28日 by 虾虾 22 Comments   最新的Windows 10 MSD ...

  9. 转载:android.屏幕锁,解锁,在取证上的应用

    中国司法-鉴定,2013年第06期杂志  

  10. hadoop集群全纪录

    169namenode 170datanode 171datenode 1:部署JDK 获取jdk安装代码:jdk-7u21-linux-x64.gz tar -zxvf jdk-7u21-linux ...