题目链接:http://poj.org/problem?id=1816

思路:建好一颗Trie树,由于给定的模式串可能会重复,在原来定义的结构体中需要增加一个vector用来记录那些以该节点为结尾的字符串的序号,然后就是匹配的过程了,需要注意的是,对于‘?'和'*',每一次都是可以匹配的,并且对于'*',还得枚举所需要匹配的长度(从0开始)。由于最后的答案可能会有重复,一开始我没有判断时候有重复的,WA了好多次=.=!!.

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAN_N = ( + );
struct Trie {
int index;
Trie *next[];
vector<int > ID; //可能会重复
Trie()
{
index = -;
memset(next, , sizeof(next));
ID.clear();
}
}; Trie *root;
int getID(char ch)
{
if (ch >= 'a' && ch <= 'z') return ch - 'a';
else if (ch == '?') return ;
return ;
}
void Insert(char *str, int pos)
{
int len = strlen(str);
Trie *p = root;
REP(i, , len - ) {
int id = getID(str[i]);
if (p->next[id] == NULL) {
p->next[id] = new Trie();
}
p = p->next[id];
}
p->index = pos;
p->ID.push_back(pos);
}
int N, M;
char str[];
vector<int > ans; void Check(Trie *&p)
{
if (p->next[]) {
if (p->next[]->index != -) {
REP(i, , (int)p->next[]->ID.size()-) ans.push_back(p->next[]->ID[i]);
}
Check(p->next[]);
}
} void dfs(Trie *&p, int pos)
{
if (pos == N) {
if (p->index != -) {
REP(i, , (int)p->ID.size()-) {
ans.push_back(p->ID[i]);
}
}
Check(p);
} else {
if (p->next[getID(str[pos])]) dfs(p->next[getID(str[pos])], pos + );
if (p->next[]) dfs(p->next[], pos + );
if (p->next[]) {
REP(i, pos, N) dfs(p->next[], i);
}
}
} int main()
{
cin >> N >> M;
root = new Trie();
REP(i, , N - ) {
scanf("%s", str);
Insert(str, i);
}
REP(i, , M - ) {
scanf("%s", str);
N = strlen(str);
dfs(root, );
if ((int)ans.size() > ) {
sort(ans.begin(), ans.end());
printf("%d", ans[]);
REP(i, , (int)ans.size()-) {
if (ans[i] != ans[i - ]) printf(" %d", ans[i]);
}
puts("");
} else
puts("Not match");
ans.clear();
}
return ;
}

poj 1816 (Trie + dfs)的更多相关文章

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

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

  2. 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)

    博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...

  3. bzoj 3439 Kpm的MC密码(Trie+dfs序+主席树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3439 [题意] 给定若干串,问一个串的作为其后缀的给定串集合中的第k小. [思路] 如 ...

  4. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  5. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  6. POJ 1564 经典dfs

    1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...

  7. POJ 1321 简单dfs

    1.POJ 1321  棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...

  8. 校内OJ 1128 词链(link)(Trie+DFS)

    1128: 词链(link) 时间限制: 1 Sec  内存限制: 64 MB 提交: 23  解决: 7 [提交][状态][讨论版] 题目描述 给定一个仅包含小写字母的英文单词表,其中每个单词最多包 ...

  9. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

随机推荐

  1. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  2. 【转】关于Class.getResource和ClassLoader.getResource的路径问题

    Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题. Class.getResource(Stri ...

  3. XPath 教程

    http://www.w3school.com.cn/xpath/xpath_syntax.asp

  4. kali安装vmware tools

    昨天在网上搜了半天如何在kail下安装tools工具,结果都没有成功,今天就来说说我是怎么安装成功的. 1.按照网上的办法只能到这一步,一直提示找不到路径,点了回车也会照常出现这样的情况. Searc ...

  5. Effective C++ -----条款30:透彻了解inlining的里里外外

    将大多数inlining限制在小型.被频繁调用的函数身上.这可使日后的调试过程和二进制升级(binary upgradability)更容易,也可使潜在的代码膨胀问题最小化,使程序的速度提升机会最大化 ...

  6. code vs1436 孪生素数 2(数论+素数的判定)

    1436 孪生素数 2  时间限制: 2 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 如m=100,n=6 则 ...

  7. ajax提交表单

    $.ajax({ type: "POST", url: action, data: $('#checkout-form').serialize(), success: functi ...

  8. Quartz 使用

    public class IndexJob:IJob//此处必须实现 IJob接口 { /// <summary> /// 具体的任务 /// </summary> /// & ...

  9. 【leetcode】Remove Nth Node From End of List(easy)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  10. 【python】入门学习(四)

    函数: 定义函数 #area.py from math import pi def area(radius): """Return the area of a circl ...