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的更多相关文章

  1. 破壳漏洞利用payload—shellshock in the wild

    FireEye关于破壳漏洞(shellshock)在现实中的利用有一篇文章: shellshock in the wild 原文较长,进行了对CGI利用的详细分析,笔者比较感兴趣的是Shellshoc ...

  2. POJ 1816 Wild Words

    Wild Words Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4412   Accepted: 1149 Descri ...

  3. 【SIGGRAPH 2015】【巫师3 狂猎 The Witcher 3: Wild Hunt 】顶级的开放世界游戏的实现技术。

    [SIGGRAPH 2015][巫师3 狂猎 The Witcher 3: Wild Hunt ]顶级的开放世界游戏的实现技术 作者:西川善司 日文链接  http://www.4gamer.net/ ...

  4. 什么是野指针?(What is a wild pointer?)

    未被初始化的变量称为野指针(wild pointer).顾名思义,我们不知道这个指针指向内存中的什么地址,使用不当程序会产生各种各样的问题. 理解下面的例子: int main() { int *p; ...

  5. POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

    题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...

  6. poj 3340 Barbara Bennett's Wild Numbers(数位DP)

    Barbara Bennett's Wild Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3153   A ...

  7. 论文速读(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-- ...

  8. 野(wild)指针与悬空(dangling)指针

    1. 什么是野指针(wild pointer)? A pointer in c which has not been initialized is known as wild pointer. 野指针 ...

  9. 野指针(Wild pointer)和悬垂指针(dangling pointer)

    详细参考如下: Dangling pointer(悬垂指针.迷途指针)和 Wild pointer(野指针) 迷途指针经常出现在混杂使用malloc() 和 free() 库调用: 当指针指向的内存释 ...

  10. ICDAR2017 Competition on Reading Chinese Text in the Wild(RCTW-17) 介绍

    阅读文章:<ICDAR2017 Competition on Reading Chinese Text in the Wild(RCTW-17)> 这篇文章是对一项中文检测和识别比赛项目( ...

随机推荐

  1. tcl/tk实例详解——修改目录下所有文件(使用一个字符串代替另外一个)

    #假设目录下全部都是普通文件,如果目录下还有子目录请#参考文章"读取文件夹下所有文件返回路径",#http://blog.csdn.net/dulixin/archive/2008 ...

  2. Magento强大的配置系统

    Magento的配置系统就像是Magento的心脏,支撑着Magento的运行.这套配置系统掌管着几乎所有"module/model/class/template/etc".它把整 ...

  3. 关于利用动态代理手写数据库连接池的异常 java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to java.sql.Connection

    代码如下: final Connection conn=pool.remove(0); //利用动态代理改造close方法 Connection proxy= (Connection) Proxy.n ...

  4. Service解析

    Service解析: 运行service有如下两种方式: StartService() 访问者退出,service仍然运行: BindService() 访问者与service绑定,访问者退出,ser ...

  5. 数据库性能优化一:SQL索引一步到位

    SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭开他的神秘面纱. 1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引 ...

  6. Servlet相关接口和Servlet的生命周期

    http://www.cnblogs.com/luotaoyeah/p/3860292.html Servlet相关接口和Servlet的生命周期 创建一个Servlet类最直接的方式是实现javax ...

  7. thinkphp之wampserver安装

    1.如何修改www目录 打开httpd.conf(wamp\bin\apache\Apache2.4.4\conf): 把DocumentRoot "c:/wamp/www" 修改 ...

  8. Object-C在Nil上调用方法

    在Object-C中,nil对象的作用等同于很多其它语言的NULL指针.不同的地方在于,在nil上调用方法不会导致程序崩溃或抛出异常. 这种技术被用在很多地方,但是对于我们来讲,最主要的就是我们不用在 ...

  9. c#对象初始化

    class test:IEquatable<test> { public int aa { get; set; } public string bb { get; set; } publi ...

  10. Ngen生成Native代码实战及优缺点分析

    先科普一下,.Net是一个用于Windows的托管代码模型,用于高效构建具有视觉上引人注目的用户体验的应用程序.但这个模型生成的代码并非可执行代码,而是由.Net公共语言运行库环境执行的IL代码.所以 ...