Trie树(模糊匹配)poj1816
来源:互联网 发布:js获取浏览器ip 编辑:程序博客网 时间:2024/06/05 21:46
Language:
Wild Words
Time Limit: 2000MS Memory Limit: 65536KTotal 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.
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.
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 4t*?h*s??e**s?*ethistheanis
Sample Output
0 1 3 0 2 4 Not match3思路:对模式串建树,然后dfs找匹配,具体见代码注释
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=1000010;int N,M;char s[50];vector<int> ans;struct node{ node *next[30]; vector<int> 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;i<n;i++) { int c=idx(s[i]); if(!p->next[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;i<p->val.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<n-1;i++)printf("%d ",ans[i]); printf("%d\n",ans[n-1]); } }}tree;int main(){ while(scanf("%d%d",&N,&M)!=EOF) { tree.clear(); for(int i=0;i<N;i++) { scanf("%s",s); tree.insert(s,i); } while(M--) { scanf("%s",s); tree.find(s); } } return 0;}
0 0
- Trie树(模糊匹配)poj1816
- Poj1816(Trie+DFS)
- POJ1816 Wild Words Trie+Dfs
- POJ1816--Wild Words(Trie+搜索)
- 2015暑假训练赛团体赛(DLX+Trie模糊匹配+费用流)
- trie树实现模糊查找
- 【HihoCoder】Trie树 (字符串匹配)
- 字符串匹配/模糊匹配(查找/模糊查找)的算法
- 模糊匹配
- 模糊匹配
- 模糊匹配
- Trie树trie(前缀)
- PKU2001(Shortest Prefixes)字符串匹配-Trie树
- Trie树应用:统计与匹配
- Trie树(字典树)_实现模糊查找(支持中文)
- Google字符串模糊匹配算法,字典树模糊查询
- Google字符串模糊匹配算法,字典树模糊查询
- Google字符串模糊匹配算法,字典树模糊查询
- HDU 2063 过山车
- spring mvc 文件上传
- HDU1528_Card Game Cheater(二分图/最大匹配)
- 【图算法之二分图】HDU 1150---Machine Schedule
- UIViewController生命周期
- Trie树(模糊匹配)poj1816
- pat gee 2013-10-7 a,b and c(64bit)
- 盒子游戏
- UILite-MFC/WTL/DirectUI界面库
- NYOJ 题目91 阶乘之和(贪心)
- Dividing (HDU 1059) —— 多重背包
- ssh配置的数据库连接不上
- Bash scripting Tutorial
- Setting property 'minSpareThreads' to '25' did not find a matching property.