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一试 首先 ...
随机推荐
- python装饰器初探
一.含有一个装饰器 #encoding: utf-8 ############含有一个装饰器######### def outer(func): def inner(*args, **kwargs): ...
- 多字段 java对象排序
public class ReflexUtil { static Logger logger = LoggerFactory.getLogger(ReflexUtil.class); //getMet ...
- nginx服务器设置url的优雅链接
对于LNMP这样架构的网站来说,一般都是基于php框架开发,php框架一般都会讲究优雅链接,比如Laravel,CodeIgniter,ThinkPHP等都是支持这种链接模式的,在服务器配置上也叫作u ...
- linux (RHEL) 添加和删除用户
linux添加新用户使用 useradd -----create a new user or update default new user information 删除用户使用userdel -- ...
- ACM/ICPC 之 SPFA练习两道(ZOJ3088-ZOJ3103)
两道题都需要进行双向SPFA,比范例复杂,代码也较长,其中第二题应该可以用DFS或者BFS做,如果用DFS可能需要的剪枝较多. ZOJ3088-Easter Holydays //利用SPFA找出下降 ...
- 8.js模式-状态模式
1. 状态模式 var offLightState = function(light){ this.light = light; } offLightState.prototype.buttonWas ...
- ffmpeg-20160510-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- [Android]检查当前手机是否有网络
// Check network connection private boolean isNetworkConnected(){ ConnectivityManager connectivityMa ...
- EF的各种删除方法
//2.1检查 id 是否存在 //2.2执行删除 Models.Student stu = new Models.Student() { Id = id }; //db.Students.Attac ...
- Odoo创建数据库时出现的问题 DataError: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
解决方案: 执行如下指令进入PostgreSQL控制台: sudo -u postgres psql postgres 然后在PostgreSQL控制下按顺序执行如下指令: update pg_dat ...