Trie树(模糊匹配)poj1816

Language: Default
Wild Words
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4681   Accepted: 1221

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
思路:对模式串建树,然后dfs找匹配,具体见代码注释

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=1000010;
int N,M;
char s[50];
vector ans;

struct node
{
    node *next[30];
    vector val;
    node(){memset(next,0,sizeof(next));}
};
struct TREE
{
    node *root;
    int len;
    void clear(){root=new node();}
    int idx(char x)
    {
        if(x=='?')return 26;
        if(x=='*')return 27;
        return x-'a';
    }
    void insert(char *s,int id)
    {
        int n=strlen(s);
        node *p=root;
        for(int i=0;inext[c])
                p->next[c]=new node();
            p=p->next[c];
        }
        p->val.push_back(id);
    }
    void dfs(char *s,node *p,int pos)
    {
        if(s[pos]!=0)//当前字符不是结束符时,有三种情况可以匹配,分别是字母,?和*
        {
            int c=idx(s[pos]);
            if(p->next[c])dfs(s,p->next[c],pos+1);
            if(p->next[26])dfs(s,p->next[26],pos+1);
            if(p->next[27])//如果是*因为它可以匹配多个,所以从可以从当前位置到最后随便选一个开始匹配
                for(int i=pos;i<=len;i++)//这里要到等于len,因为这个*可以直接匹配所有省下的字符
                    dfs(s,p->next[27],i);
        }
        else
        {
            for(int i=0;ival.size();i++)ans.push_back(p->val[i]);
            if(p->next[27])dfs(s,p->next[27],pos);//因为*可以不匹配,所以如果s传结束了,但是模式串可能还没结束
        }
    }
    void find(char *s)
    {
        len=strlen(s);
        ans.clear();
        dfs(s,root,0);
        sort(ans.begin(),ans.end());
        ans.resize(distance(ans.begin(),unique(ans.begin(),ans.end())));
        int n=ans.size();
        if(!n)printf("Not match\n");
        else
        {
            for(int i=0;i




你可能感兴趣的:(DFS,字典树,Trie树,前缀树)