Wild Words
poj1816:http://poj.org/problem?id=1816
题意:给你n个模板串,然后每个串除了字母,还有?或者*,?可以代替任何非空单个字符,*可以替代任何长度任何串,包括空字符串。现在给以一些串,问你这些串在哪些串中出现过。
题解:trie+DFS。首先,把n个字符串放到trie中,注意,这n个串中可能有相同的字符串。然后对于每个要查询的串,从根节点进行DFS搜索,注意一些特殊处理,例如匹配到*或者?的处理。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 500001
using namespace std;
const int cha=;
struct node{
int num;
int count[];//从根到此处是否是关键字,并且记录是多少个关键字的结尾
int next[cha];
}tree[N];
int ans[],top;
int n,m;
void init(node &a,int data){
a.num=;
for(int i=;i<cha;i++)
a.next[i] = -;
}
int k = ;
void insert(char s[],int num){
int p = ;
for(int i=;s[i];i++){
int data = s[i]-'a';
if(s[i]=='?')data=;
if(s[i]=='*')data=;
if(tree[p].next[data]==-){//不存在该结点
init(tree[k],num);
tree[p].next[data] = k;
k++;
}
p = tree[p].next[data];
}
int temp=tree[p].num;
tree[p].num++;
tree[p].count[++temp]=num;
}
void DFS(node p,char *s,int len,int cur){
if(cur==len){
if(p.num){
for(int i=;i<=p.num;i++)
ans[++top]=p.count[i];
}
while(p.next[]>&&tree[p.next[]].num==)
p=tree[p.next[]];
if(p.next[]>&&tree[p.next[]].num>){
int tt=tree[p.next[]].num;
for(int i=;i<=tt;i++)
ans[++top]=tree[p.next[]].count[i];
}
return ;
}
int t=s[cur]-'a';
if(p.next[t]>)
DFS(tree[p.next[t]],s,len,cur+);
if(p.next[]>)
DFS(tree[p.next[]],s,len,cur+);
if(p.next[]>){
int temp=cur;
while(temp<=len){
DFS(tree[p.next[]],s,len,temp);
temp++;
}
}
}
char str[];
int main(){
while(~scanf("%d%d",&n,&m)){
init(tree[],-);
for(int i=;i<=n;i++){
scanf("%s",str);
insert(str,i-);
}
for(int i=;i<=m;i++){
scanf("%s",str);
top=;
DFS(tree[],str,strlen(str),);
sort(ans+,ans+top+);
int tt=unique(ans+,ans+top+)-ans-;
if(tt>){
for(int i=;i<tt;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[tt]);
}
else
printf("Not match\n");
}
}
}
Wild Words的更多相关文章
- 破壳漏洞利用payload—shellshock in the wild
FireEye关于破壳漏洞(shellshock)在现实中的利用有一篇文章: shellshock in the wild 原文较长,进行了对CGI利用的详细分析,笔者比较感兴趣的是Shellshoc ...
- POJ 1816 Wild Words
Wild Words Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4412 Accepted: 1149 Descri ...
- 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。
[SIGGRAPH 2015][巫师3 狂猎 The Witcher 3: Wild Hunt ]顶级的开放世界游戏的实现技术 作者:西川善司 日文链接 http://www.4gamer.net/ ...
- 什么是野指针?(What is a wild pointer?)
未被初始化的变量称为野指针(wild pointer).顾名思义,我们不知道这个指针指向内存中的什么地址,使用不当程序会产生各种各样的问题. 理解下面的例子: int main() { int *p; ...
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...
- poj 3340 Barbara Bennett's Wild Numbers(数位DP)
Barbara Bennett's Wild Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3153 A ...
- 论文速读(Jiaming Liu——【2019】Detecting Text in the Wild with Deep Character Embedding Network )
Jiaming Liu--[2019]Detecting Text in the Wild with Deep Character Embedding Network 论文 Jiaming Liu-- ...
- 野(wild)指针与悬空(dangling)指针
1. 什么是野指针(wild pointer)? A pointer in c which has not been initialized is known as wild pointer. 野指针 ...
- 野指针(Wild pointer)和悬垂指针(dangling pointer)
详细参考如下: Dangling pointer(悬垂指针.迷途指针)和 Wild pointer(野指针) 迷途指针经常出现在混杂使用malloc() 和 free() 库调用: 当指针指向的内存释 ...
- ICDAR2017 Competition on Reading Chinese Text in the Wild(RCTW-17) 介绍
阅读文章:<ICDAR2017 Competition on Reading Chinese Text in the Wild(RCTW-17)> 这篇文章是对一项中文检测和识别比赛项目( ...
随机推荐
- 在iframe中获取父页面的元素
a.html <!DOCTYPE html> <html> <head> <title></title> </head> < ...
- WinForm界面(一)
一:Form对象 属性: 设计中的Name:窗体类的类名AcceptButton:窗口的确定按钮CancelButton:窗口按ESC的取消按钮 1.外观 Backcolor:背景颜色Forecolo ...
- Linux操作系统安装Nvidia显卡驱动
一直以来,Linux分支系统使用过程中都有驱动适配麻烦,完全适配的驱动也不多.对于Nvidia显卡而言,一般Linux各分支操作系统虽然提供了N卡开源驱动工程Nouveau,但是性能上还是有待提高.下 ...
- 关于onSaveInstanceState的javadoc的渣渣翻译
/** * Called to retrieve per-instance state from an activity before being * killed so that the state ...
- HDU 2501 Tiling_easy version(简单递推)
Tiling_easy version Problem Description 有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有 ...
- LA 3708 Graveyard(推理 参考系 中位数)
Graveyard Programming contests became so popular in the year 2397 that the governor of New Earck -- ...
- bzoj2732: [HNOI2012]射箭 半平面交
这题乍一看与半平面交并没有什么卵联系,然而每个靶子都可以转化为两个半平面. scanf("%lf%lf%lf",&x,&ymin,&ymax); 于是乎就有 ...
- ASP.NET数据绑定控件
数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...
- ubuntu fcitx 安装 使用
系统内置的ibus的86五笔,感觉有些老不太好用, 所以安装试用了一下fcitx下的五笔,记录一下安装方法 ,各种搜索... 我的ubuntn版本: #48-Ubuntu SMP Fri Aug 24 ...
- Shell符号展开
字符 展开 * 这个 “*” 字符意味着匹配文件名中的任意字符 shell 把 “*” 展开成了另外的东西 ,在 echo 命令被执行前. ~家目录 算术表达式展开 算术表达式展开使用这种格式: $( ...