Wild Words POJ - 1816
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爆搜就行,刚开始一直TLE,后来发现是SET的事
题意:上边给N个类似正则表达式的玩意,‘*’表示匹配0个或多个字母,‘?’表示一个,问有哪几种是能匹配到的,全部不能NOT MATCH
#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<sstream>
#include<string>
using namespace std;
#define inf 0x3f3f3f3f
struct node
{
node* next[28];
int mos[101];
int ges;
// vector<int>q;
}ss[100000];
int o;
inline node *add()
{
node *t=&ss[o++];
memset(t->next,0,sizeof(t->next));
t->ges=0;
// t->q.clear();
return t;
}
void inse(string a,node *root,int pp)
{
int len=a.length();
for(int i=0;i<len;i++)
{
if(a[i]>='a'&&a[i]<='z')
{
int tt=a[i]-'a';
if(root->next[tt])
{
root=root->next[tt];
}
else
{
node *t=add();
// node *t=&ss[o++];
// memset(t->next,0,sizeof(t->next));
// t->ges=0;
// t->q.clear();
root->next[tt]=t;
root=root->next[tt];
}
}
else
{
if(a[i]=='?')
{
if(root->next[26])
{
root=root->next[26];
}
else
{
node *t=add();
// node *t=&ss[o++];
//memset(t->next,0,sizeof(t->next));
// t->ges=0;
// t->q.clear();
root->next[26]=t;
root=root->next[26];
}
}
else
{
if(root->next[27])
{
root=root->next[27];
}
else
{
node *t=add();
// node *t=&ss[o++];
// memset(t->next,0,sizeof(t->next));
// t->ges=0;
// t->q.clear();
root->next[27]=t;
root=root->next[27];
}
}
}
}
root->mos[(root->ges)++]=pp;
//root->q.push_back(pp);
}
//set<int>qq;
int ans[100000];
int ans_sum;
void dfs(char a[],node *root)
{
if(a[0]==0)
{
while(root)
{
int len=root->ges;
// int len=root->q.size();
for(int i=0;i<len;i++)
{
//qq.insert(root->q[i]);
//ans[ans_sum++]=root->q[i];
ans[ans_sum++]=root->mos[i];
}
root=root->next[27];
}
return;
}
if(root->next[26]!=NULL)
{
dfs(a+1,root->next[26]);
}
if(root->next[27]!=NULL)
{
int len=strlen(a);
for(int i=0;i<=len;i++ )
{
dfs(a+i,root->next[27]);
}
}
if(root->next[a[0]-'a'])
{
dfs(a+1,root->next[a[0]-'a']);
}
}
int main()
{
int n,m;
o=0;
cin>>n>>m;
node *root=add();
// node *root=&ss[o++];
// memset(root->next,0,sizeof(root->next));
// t->ges=0;
// root->q.clear();
//=add();
int pp=0;
while(n--)
{ string a;
cin>>a;
inse(a,root,pp++);
}
char b[1000];
while(m--)
{
//qq.clear();
cin>>b;
ans_sum=0;
dfs(b,root);
if(ans_sum==0)
{
cout<<"Not match"<<endl;
continue;
}
//set<int>::iterator it;
//for(it=qq.begin();it!=qq.end();it++)
//cout<<*it<<" ";
//cout<<endl;
sort(ans,ans+ans_sum);
cout<<ans[0];
for(int i=1;i<ans_sum;i++)
if(ans[i]!=ans[i-1])
cout<<" "<<ans[i];
cout<<endl;
}
return 0;
}