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. Nginx学习笔记(四) 源码分析&socket/UDP/shmem

    源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_socket.h&Ngx_s ...

  2. BlueDream.js(蓝梦)——jQuery网站使用引导插件

    小菜在前端世界游荡有些时间了,常见的插件多少有些了解,但却很少看到用户引导插件. 所谓用户引导插件,就是在第一次使用某个网站时,会弹出一些小动画,告诉你网站的基本使用方法,帮你快速入门. 这应该是个常 ...

  3. 说不尽的MVVM(5) - 消息满天飞

    知识预备 阅读本文,我假定你具备以下知识: C#和WPF基础知识 Lambda表达式 清楚ViewModel的职责 如果我们的程序需要弹出一个MessageBox,我们应该怎么做? 我见过不少人在Vi ...

  4. maven nexus 3 third party 构件上传

    mvn -e deploy:deploy-file -DgroupId=com.oracle -DartifactId=JDBCDriver -Dversion=12.0.1 -Dpackaging= ...

  5. 记一次https访问握手失败(handshake failure)

    文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6239518.html  转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...

  6. js程序设计01——基本概念

    本文为js高级程序设计学习笔记,笔记中不乏本人学习js的一些心得demo,喜欢的朋友可以直接参考原书“javascript高级程序设计”,写本笔记的目的是对js中容易出错.不易理解的地方作个笔记,以免 ...

  7. 用自己的算法实现startsWith和endsWith功能

    package hanqi; import java.util.Random; import java.util.Scanner; public class zuoye { public static ...

  8. VS SuppressMessage忽略特定方法的警告信息

    VS在编译源码的时候有很多警告信息,有些时候 我们需要忽略一个特定方法的特定警告信息,于是就用SuppressMessage特性,可是这个特性的参数不太好搞定,还好有VS,Suppressing Co ...

  9. Find Minimum in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  10. Java web实时进度条整个系统共用(如java上传进度条、导入excel进度条等)

    先上图: 这上文件上传的: 这是数据实时处理的: 1:先说说什么是进度条:进度条即计算机在处理任务时,实时的,以图片形式显示处理任务的速度,完成度,剩余未完成任务量的大小,和可能需要处理时间,显示方式 ...