poj 1816 (Trie + dfs)
题目链接: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)的更多相关文章
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...
- bzoj 3439 Kpm的MC密码(Trie+dfs序+主席树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3439 [题意] 给定若干串,问一个串的作为其后缀的给定串集合中的第k小. [思路] 如 ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- POJ 1321 简单dfs
1.POJ 1321 棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...
- 校内OJ 1128 词链(link)(Trie+DFS)
1128: 词链(link) 时间限制: 1 Sec 内存限制: 64 MB 提交: 23 解决: 7 [提交][状态][讨论版] 题目描述 给定一个仅包含小写字母的英文单词表,其中每个单词最多包 ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
随机推荐
- ios NSNotificationCenter 收到通知后的执行线程
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Thread ...
- web开发架构设计
2015-07-31 13:10:38 一, web服务器 .负载均衡 .不做对URL的rewrite逻辑判断, 全部转发到代码服务器的单一入口文件, 由代码去全权处理 二, 代码服务器(常跟web服 ...
- Effective C++ -----条款24:若所有参数皆需类型转换,请为此采用non-member函数
如果你需要为某个函数的所有参数(包括被this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.
- Mathematics:Dead Fraction(POJ 1930)
消失了的分式 题目大意:某个人在赶论文,需要把里面有些写成小数的数字化为分式,这些小数是无限循环小数(有理数),要你找对应的分母最小的那个分式(也就是从哪里开始循环并不知道). 一开始我也是蒙了,这尼 ...
- jquery的基本事件大全
].name); });jQuery.getScript( url, [callback] ) 使用GET请求javascript文件并执行. $.getScript(”test.js”, funct ...
- 最详细的 Android Toolbar 开发实践总结
http://www.codeceo.com/article/android-toolbar-develop.html 参考此链接!
- 自定义ToolBar之一
其实已经有很多大神写过这方面的文章了,不过我比较蠢吧,老有一些地方看不懂的,翻了很多关于Toolbar方面的文章和视频,这儿总结一下. 参考资料:youtube:slidenerd 阶段一 自定义配 ...
- 【leetcode】Remove Duplicates from Sorted List (easy)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Struts 2, 值在验证失败回到原来页面的时候会丢失的解决方案
比如,当我要添加一个信用卡的时候,我需要信用卡的CardType, 这些数据是存在数据库中的,我要先通过action 的一个 create方法,取到信用卡的类型: public String crea ...
- ServletConfig与ServletContext
ServletConfig与ServletContext对象详解 一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...