http://poj.org/problem?id=1816

比较麻烦的trie。

首先你需要选择针对n还是m建立trie,这里我选择了针对n。

那么就需要面临卡空间的问题。

这里提供了一种链式前向星的方法能够当你不会指针trie的时候卡过空间。(做法看代码吧)

然后针对m进行在trie上的dfs即可。

对于相同字符或?来说,trie下移1位,匹配串移动1位。

对于*来说,trie下移,匹配串移动0~长度位。

(合计这道题就难在模拟上了)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=;
char s[];
struct node{
char se;
int to;
int nxt;
vector<int>ed;
}edge[maxn];
int head[maxn],cnt=,cnt1=;
void add(int u,char c){
cnt++;cnt1++;
edge[cnt].se=c;
edge[cnt].to=cnt1;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
void insert(int k){
int u=;
int l=strlen(s);
for(int i=;i<l;i++){
int v=-;
char c=s[i];
for(int j=head[u];j;j=edge[j].nxt){
if(edge[j].se==c){
v=edge[j].to;
if(i==l-)edge[j].ed.push_back(k);
break;
}
}
if(v<){
add(u,c);
v=cnt1;
if(i==l-)edge[cnt].ed.push_back(k);
}
u=v;
}
return;
}
vector<int>ans;
int l;
void check(int u,int k,int p){
if(k==l){
if(!edge[p].ed.empty()){
for(int i=;i<edge[p].ed.size();i++){
ans.push_back(edge[p].ed[i]);
}
}
for(int j=head[u];j;j=edge[j].nxt){
int v=edge[j].to;
if(edge[j].se=='*'){
check(v,k,j);
}
}
return;
}
char c=s[k];
for(int j=head[u];j;j=edge[j].nxt){
int v=edge[j].to;
if(edge[j].se==c||edge[j].se=='?'){
check(v,k+,j);
}
if(edge[j].se=='*'){
for(int q=;q<=l-k;q++){
check(v,k+q,j);
}
}
}
return;
}
bool t[];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",s);
insert(i);
}
for(int j=;j<m;j++){
scanf("%s",s);
ans.clear();
l=strlen(s);
check(,,);
if(ans.empty()){
printf("Not match");
}else{
memset(t,,sizeof(t));
sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++){
if(!t[ans[i]]){
printf("%d ",ans[i]);
t[ans[i]]=;
}
}
}
printf("\n");
}
return ;
}

POJ1816:Wild Words——题解的更多相关文章

  1. poj1816 Wild Words

    Wild Words Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5567   Accepted: 1475 Descri ...

  2. Clash Credenz 2014 Wild Card Round题解

    A题 简单模拟. /************************************************************************* > File Name: ...

  3. Wild Words

    poj1816:http://poj.org/problem?id=1816 题意:给你n个模板串,然后每个串除了字母,还有?或者*,?可以代替任何非空单个字符,*可以替代任何长度任何串,包括空字符串 ...

  4. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  5. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  6. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  7. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  8. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  9. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

随机推荐

  1. MySQL - 问题集 - Access denied; you need the SUPER privilege for

    当执行存储过程相关操作时,如果出现该错误,则往下看. 打开存储过程,会发现“CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost`”. 由于DEFI ...

  2. 引领技术变革,腾讯云、腾讯WeTest和英特尔,合作布局云游戏

    WeTest 导读 ChinaJoy作为中国泛娱乐产业年度风向标,受到全球业界的高度关注.在本届ChinaJoy上,腾讯云.腾讯WeTest和英特尔,合作为游戏玩家.游戏开发者等业界人士联合展出了云游 ...

  3. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

  4. thinkphp5使用workerman的定时器定时任务在某一个时间执行

    1.首先通过 composer 安装workerman,在thinkphp5完全开发手册的扩展->coposer包->workerman有详细说明: #在项目根目录执行以下指令compos ...

  5. 为什么测试人员必须掌握Linux?

    相信点进来的小伙伴不是对Linux感兴趣就是对测试感兴趣了,也希望本文可以帮助之前接触过Linux的小伙伴找到继续坚持学习下去的动力,之前没接触过Linux的小伙伴也能找到开始学习Linux的兴趣. ...

  6. MySQL☞lower函数

    lower(列名/字符串):将大写字母改成小写字母 格式: select  lower(列名/字符串)  from  表名 如下图:

  7. 那些年我们不爱学的mysql单词

    MySQL 一种关系型数据库 database 数据库,简称DB databases 数据库的复数,代表多个数据库 net 网络/服务 start 启动 stop 停止 root MySQL数据库中的 ...

  8. TPO-13 C1 Understand the assignment in psychology course

    TPO-13 C1 Understand the assignment in psychology course 第 1 段 1.listen to a conversation between a ...

  9. 【radio-group、radio】 单选项组件说明

    radio-group组件是包裹radio组件的容器 原型: <radio-group bindchange="[EventHandle]"> <radio .. ...

  10. jQuery 调用后台方法(net)

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...