poj 1816 Trie+DFS匹配模式串

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define maxn 600010
struct Trie
{
    int next[maxn][28];
    vector 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




你可能感兴趣的:(搜索,数据结构,字符串,Trie,DFS)