POJ 1816 字典树+DFS

题意

类似正则匹配,*匹配0-n个字符,?匹配1个字符。给一个字符串,问哪几个匹配表达式能匹配这个字符串。

题解

如同AC自动机和DP完美结合一样,字典树和DFS也是完美结合。针对每一个字符串,结合字典树进行DFS。DFS深搜的时候,对?和*的情况进行特殊处理,如果存在?节点,则字符串匹配位置向后移动一位。如果存在*节点,则字符串匹配位置向后移动一位,或者不移动。由于*可以匹配多个字符,因此对于*允许持续向后移动字符串匹配位置。
最后如果匹配到字符串终止位置,则记录匹配节点所对应的编号。最后输出记录集合即可。

注意事项

开60万*28的数组会MLE,于是试着开小了一点,10万*28成功AC。感觉这个内存卡的很无聊。。

代码

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<string>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<string>
#define UP(i,l,h) for(int i=l;i<h;i++)
#define DOWN(i,h,l) for(int i=h-1;i>=l;i--)
#define W(a) while(a)
#define MEM(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MAXN 100010
#define MOD 1000000009
#define EPS 1e-10
using namespace std;

int ch[100010][28];
char st[30];
bool vis[100010];
vector<int> ans;
vector<int> val[100010];
bool star[100010];
int sz,valpos;
int stlen;

int getNum(char c) {
    if(c=='?') {
        return 26;
    } else if(c=='*') {
        return 27;
    } else {
        return c-'a';
    }
}

void insert(int x) {
    int u=0;
    int len=strlen(st);
    UP(i,0,len) {
        int x=getNum(st[i]);
        if(!ch[u][x]) {
            ch[u][x]=sz++;
        }
        u=ch[u][x];
        if(x==27) {
            star[u]=true;
        }
    }
        val[u].push_back(x);
//        cout<<x<<" "<<u<<endl;
}

void find(int len,int u) {
    if(len==stlen) {
//            cout<<mp[u]<<endl;
        int sz=val[u].size();
        UP(i,0,sz) {
            if(!vis[val[u][i]]) {
                vis[val[u][i]]=true;
                ans.push_back(val[u][i]);
            }
        }
        if(ch[u][27]) {
            find(len,ch[u][27]);
        }
        return ;
    }
    int x=getNum(st[len]);
    if(ch[u][x]) {
        find(len+1,ch[u][x]);
    }
    if(ch[u][27]) {
        find(len+1,ch[u][27]);
        find(len,ch[u][27]);
    }
    if(ch[u][26]) {
        find(len+1,ch[u][26]);
    }
    if(star[u]) {
        find(len+1,u);
    }
}
int main() {
    int n,m;
    W(~scanf("%d%d",&n,&m)) {
        MEM(ch,0);
        MEM(val,0);
        MEM(star,false);
        sz=1;
        valpos=0;
        UP(i,0,n) {
            scanf("%s",st);
            insert(i);
        }
        UP(i,0,m) {
            ans.clear();
            MEM(vis,false);
            scanf("%s",st);
            stlen=strlen(st);
            find(0,0);
            int sz=ans.size();
            if(sz>0) {
                sort(ans.begin(),ans.end());
                UP(i,0,sz) {
                    if(i==sz-1)
                        printf("%d\n",ans[i]);
                    else
                        printf("%d ",ans[i]);
                }
            } else {
                puts("Not match");
            }
        }
    }
}

/*
7 4
t*
?h*s
??e*
??e*
??e*
*s
?*e
is
an
the
this
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值