poj 1816 Trie+DFS匹配模式串

9 篇文章 0 订阅
7 篇文章 0 订阅

Wild Words
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4887 Accepted: 1274

Description

A word is a string of lowercases. A word pattern is a string of lowercases, '?'s and '*'s. In a pattern, a '?' matches any single lowercase, and a '*' matches none or more lowercases.

There are many word patterns and some words in your hand. For each word, your task is to tell which patterns match it.

Input

The first line of input contains two integers N (0 < N <= 100000) and M (0 < M <=100), representing the number of word patterns and the number of words. Each of the following N lines contains a word pattern, assuming all the patterns are numbered from 0 to N-1. After those, each of the last M lines contains a word.

You can assume that the length of patterns will not exceed 6, and the length of words will not exceed 20.

Output

For each word, print a line contains the numbers of matched patterns by increasing order. Each number is followed by a single blank. If there is no pattern that can match the word, print "Not match".

Sample Input

5 4
t*
?h*s
??e*
*s
?*e
this
the
an
is

Sample Output

0 1 3 
0 2 4 
Not match
3


匹配模式串,类似于正则表达式,学编译原理的时候记得匹配字符串要构建一个状态机,在纸上画了画大概跟AC自动机差不多,但是只要Trie就够了,用Trie建立好状态机,然后DFS匹配就可以了,需要特殊处理的是*的匹配,可以匹配为空或者任意多个。注意几个坑点 1.模式串可能有重复的 2.按升序输出-.- 3.同一个字符串匹配同一个模式可能有重复的方法(比如*a*a和aaa)


#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;

#define maxn 600010
struct Trie
{
    int next[maxn][28];
    vector<int> end[maxn]; //end用于存储到该节点的模式的下标,用vector是因为可能有多个重复的模式
    int tot,rt;
    int newnode()
    {
        memset(next[tot], -1, sizeof(next[tot]));
        end[tot++].clear();
        return tot-1;
    }
    void init()
    {
        tot=0;
        rt=newnode();
    }

    void insert(char *s, int tag)
    {
        int len=strlen(s);
        int cur=rt;
        for(int i=0; i<len; i++){
            if(next[cur][s[i]-'a']==-1)
                next[cur][s[i]-'a']=newnode();
            if(s[i]-'a'==27)    //假如是*,则可以循环回到自己
                next[next[cur][27]][27]=next[cur][27];
            cur=next[cur][s[i]-'a'];
        }
        end[cur].push_back(tag);
    }

    //s代表待匹配串,mat代表已匹配字符个数,cur当前所处节点, a数组保存匹配的模式下标,len为a的个数
    //used为了记录特殊状态,*匹配为空或者匹配为任意多个字符
    void query(char *s, int mat, int cur, int used, int *a, int &len) 
    {
        if(cur==-1) return;
        if(mat==strlen(s)){
            if(end[cur].size())
                for(int i=0; i<end[cur].size(); i++) a[len++]=end[cur][i];
            if(!used)
            query(s, mat, next[cur][27], 2, a, len);
            return;
        }

        query(s, mat+1, next[cur][s[mat]-'a'], 0,a, len);
        query(s, mat+1, next[cur][26], 0,a, len); //状态26匹配?任意一个字符
        if(used!=2)
        query(s, mat+1, next[cur][27], 1,a, len); //状态27匹配*任意多个字符,前提是上一次不能选择*匹配为空
        if(!used)
        query(s, mat, next[cur][27], 2, a, len);  //状态27匹配*空,标记为2,前提是上一次不能选择*匹配任意字符

    }
};

void turn(char *s)
{
    int len=strlen(s);
    for(int i=0; i<len; i++){
        if(s[i]=='?')           
            s[i]='z'+1;
        else if(s[i]=='*')
            s[i]='z'+2;
    }
}

char buf[105];
int a[100005],len;
Trie trie;
int main()
{
    int n,m;
    while(scanf("%d%d", &n, &m)==2){
        trie.init();
        for(int i=0; i<n; i++){
            scanf("%s", buf);
            turn(buf);
            trie.insert(buf,i);
        }

        for(int i=0; i<m; i++){
            scanf("%s", buf);
            len=0;
            trie.query(buf, 0, 0, 0,a, len);
            sort(a,a+len); //按升序输出
            len=unique(a,a+len)-a; //同一个模式可能用不同方式匹配了需要去重
            for(int j=0; j<len; j++){
                printf("%d", a[j]);
                printf(" ");
            }
            if(!len) printf("Not match");
            puts("");
        }
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值