POJ 1816 Wild Words(trie 树上的DFS)

2024-02-19 20:48
文章标签 poj dfs 树上 words wild trie 1816

本文主要是介绍POJ 1816 Wild Words(trie 树上的DFS),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!




Wild Words
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4377 Accepted: 1142

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

Source

POJ Monthly


个人觉得这个题目是个好题目,但是测试数据有点让人吐血,有的AC代码有的测试数据根本通过不了,也能AC

这个题目我纠结了三个多小时,其实程序半小时就搞定了,谁能想到测试数据的样例有重复,而且重复的也要分顺序

输出!开始在trie树上每次保存一个,后来用vector保存多个结果,这才A

解题思路:

  在pattern上建立trie树,每个节点保存出现的编号(编号可能不唯一),然后建立完成之后在trie树上进行搜索

那么就OK了

搜索的时候分三种情况,如果当前位置 ? 可以那么直接跳过当前节点

如果 *可以,那么一直跳 0 1 2 3 结束

剩下的正常搜索

还要注意一种情况就是当字符串匹配结束后不能停止,还要判断后面有没有全是**的pattern

这个题目是个非常好的题目,测试数据也没什么大问题,就是你有重复倒是给个提示撒!


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;
struct trie
{
trie *next[28];
vector<int> num;
}re_root,po[100000];
int pos,global_num;
int ans[100010];
int ans_pos;
int init(trie *root)
{
memset(root->next,0,sizeof(root->next));
root->num.clear();
return 0;
}
int insert_trie(trie *root,char *name)
{
if(name[0]==0)
{root->num.push_back(global_num);global_num++;//return 0;
}
if(name[0] >='a' && name[0] <='z')
{
if(root->next[name[0]-'a'])
insert_trie(root->next[name[0]-'a'],name+1);
else
{
root->next[name[0]-'a']=&po[pos];
init(&po[pos]);
pos++;
insert_trie(root->next[name[0]-'a'],name+1);
}
}
else
{
if(name[0]=='?')
{
if(root->next[26])
insert_trie(root->next[26],name+1);
else
{
root->next[26]=&po[pos];
init(&po[pos]);
pos++;
insert_trie(root->next[26],name+1);
}
}
else
{
if(root->next[27])
insert_trie(root->next[27],name+1);
else
{
root->next[27]=&po[pos];
init(&po[pos]);
pos++;
insert_trie(root->next[27],name+1);
}
}
}
return 0;
}
int query(trie *root,char *name)
{
int i,j,k;
if(name[0]==0 && !root->num.empty())
{
for(i=0;i<root->num.size();i++)
ans[ans_pos++]=root->num[i];
}
if(name[0]==0)
{
//root=root->next[27];
while(root)
{
if(!root->num.empty())
{
for(i=0;i<root->num.size();i++)
ans[ans_pos++]=root->num[i];
}
root=root->next[27];
}
return 0;
}
if(root->next[27])
{
for(i=0;name[i];i++)
{
query(root->next[27],name+i);
}
query(root->next[27],name+i);
}
if(root->next[26])
query(root->next[26],name+1);
if(root->next[name[0]-'a'])
query(root->next[name[0]-'a'],name+1);
return 0;
}
int n,m;
char str[100];
bool cmp(const int &a,const int &b)
{
return a < b;
}
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
pos=0;
init(&re_root);
global_num=1;
for(i=0;i<n;i++)
{
//global_num=i+1;
scanf("%s",str);
insert_trie(&re_root,str);
}
for(i=0;i<m;i++)
{
ans_pos=0;
scanf("%s",str);
query(&re_root,str);
if(ans_pos==0)
printf("Not match");
else
{
sort(ans,ans+ans_pos,cmp);
printf("%d",ans[0]-1);
for(j=1;j<ans_pos;j++)
if(ans[j]!=ans[j-1])
printf(" %d",ans[j]-1);
}
printf("\n");
}
}
return 0;
}


这篇关于POJ 1816 Wild Words(trie 树上的DFS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/725916

相关文章

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一