【HDOJ】2279 File Search Tool
显然适用字典树建树,串长和模式串都很小,所以直接递归搜索。同时,适用bk标记当前的查询次数(排除不同模式的多次查询成功,如*t*)。需要主要的是,居然存在同名文件!!!。
/* 2279 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
using namespace std; #define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct node_t {
int in;
int c;
int next[];
} node_t; const int maxl = ;
const int maxn = ;
node_t nd[maxn*maxl];
char s[maxl], d[maxl];
int dl, sl;
int L = maxn*maxl, bk;
int n, m, ans; void init() {
memset(nd, , sizeof(node_t)*L);
L = bk = ;
} void insert() {
int i = , id;
int p = , q; while (s[i]) {
id = s[i] - 'a';
q = nd[p].next[id];
if (q == )
q = nd[p].next[id] = L++;
p = q;
++i;
}
++nd[p].c;
} void search(int p, int l) {
if (l == dl) {
if (nd[p].c && nd[p].in!=bk) {
ans += nd[p].c;
nd[p].in = bk;
}
return ;
} if (d[l] == '?') {
for (int i=; i<; ++i)
if (nd[p].next[i])
search(nd[p].next[i], l+); } else if (d[l] == '*') {
search(p, l+);
for (int i=; i<; ++i)
if (nd[p].next[i])
search(nd[p].next[i], l); } else {
int id = d[l] - 'a';
if (nd[p].next[id])
search(nd[p].next[id], l+); }
} void handle() {
int i = ; sl = strlen(s);
dl = ;
while () {
while (s[i]=='*'&&s[i+]=='*')
++i;
if (i >= sl)
break;
d[dl++] = s[i++];
}
d[dl] = '\0';
} int main() {
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d %d",&n,&m)!=EOF) {
init();
for (i=; i<n; ++i) {
scanf("%s", s);
insert();
}
while (m--) {
scanf("%s", s);
handle();
ans = ;
search(, );
++bk;
if (ans)
printf("%d\n", ans);
else
puts("Not match");
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】2279 File Search Tool的更多相关文章
- 【HDOJ】2222 Keywords Search
AC自动机基础题. #include <iostream> #include <cstdio> #include <cstring> #include <cs ...
- 【python】类file文件处理
[flush方法] 通常由于缓冲,write不将数据写入文件,而是写入内存的缓冲区,需要使用flush写入文件,并清空缓冲区 文件的flush方法的作用是强制清空缓存写入文件.默认每行flush一下? ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【HDU】2222 Keywords Search
[算法]AC自动机 [题解]本题注意题意是多少关键字能匹配而不是能匹配多少次,以及可能有重复单词. 询问时AC自动机与KMP最大的区别是因为建立了trie,所以对于目标串T与自动机串是否匹配只需要直接 ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】1547 Bubble Shooter
两次BFS,一次扫描关联点.一次扫描可能掉落的情况(即再次扫描所有非爆炸的联通点).余下未被扫描的点均爆炸. #include <cstdio> #include <cstring& ...
- 【转】MAT(Memory Analyzer Tool)工具入门介绍
1.MAT是什么? MAT(Memory Analyzer Tool),一个基于Eclipse的内存分析工具,是一个快速.功能丰富的JAVA heap分析工具,它可以帮助我们查找内存泄漏和减少内存消耗 ...
随机推荐
- nginx之服务器
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- ios>android>javaee
外面就业很多年的认识给出来我这个结论,请问各位有什么看法?
- .NET3.5中的高性能 Socket API
转载:http://www.cnblogs.com/TianFang/archive/2007/11/09/954730.html 在 .NET Framework 2.0 版本中,System.Ne ...
- declare-styleable:自定义控件的属性
http://www.cnblogs.com/jisheng/archive/2013/01/10/2854891.html 在使用过程中, 1 TypedArray a = getContext() ...
- Linq 中的distinct去重
Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...
- .NET 操作PDF文档以及PDF文件打印摸索总结
关于生成 PDF 的操作,相信大家的在实际的工作过程中难免会碰到.以前我们通过生成 word 文档来进行文件的打印,但是由于太过依赖 office 软件,因此尝试能不能使用 PDF 进行文件打印. 在 ...
- SQL通过传递参数方式备份数据库.
存储过程的SQL代码: ALTER PROCEDURE USP_DBBackup ), --存储目录. ) --存储数据库名. AS SET NOCOUNT ON ) select @name = r ...
- jsoup:解析HTML用法小结
1.解析方式 (1)从字符串解析 ? 1 2 3 String html = "<html><head><title>First parse</ti ...
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- webapp框架—学习AngularUI1(demo折腾)
angularUI下载地址:https://github.com/Clouda-team/BlendUI 下载解压后,demo在根目录 现在测试官网demo的使用 用浏览器打开mobile-angul ...