POJ1816:Wild Words——题解
http://poj.org/problem?id=1816
比较麻烦的trie。
首先你需要选择针对n还是m建立trie,这里我选择了针对n。
那么就需要面临卡空间的问题。
这里提供了一种链式前向星的方法能够当你不会指针trie的时候卡过空间。(做法看代码吧)
然后针对m进行在trie上的dfs即可。
对于相同字符或?来说,trie下移1位,匹配串移动1位。
对于*来说,trie下移,匹配串移动0~长度位。
(合计这道题就难在模拟上了)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=;
char s[];
struct node{
char se;
int to;
int nxt;
vector<int>ed;
}edge[maxn];
int head[maxn],cnt=,cnt1=;
void add(int u,char c){
cnt++;cnt1++;
edge[cnt].se=c;
edge[cnt].to=cnt1;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
void insert(int k){
int u=;
int l=strlen(s);
for(int i=;i<l;i++){
int v=-;
char c=s[i];
for(int j=head[u];j;j=edge[j].nxt){
if(edge[j].se==c){
v=edge[j].to;
if(i==l-)edge[j].ed.push_back(k);
break;
}
}
if(v<){
add(u,c);
v=cnt1;
if(i==l-)edge[cnt].ed.push_back(k);
}
u=v;
}
return;
}
vector<int>ans;
int l;
void check(int u,int k,int p){
if(k==l){
if(!edge[p].ed.empty()){
for(int i=;i<edge[p].ed.size();i++){
ans.push_back(edge[p].ed[i]);
}
}
for(int j=head[u];j;j=edge[j].nxt){
int v=edge[j].to;
if(edge[j].se=='*'){
check(v,k,j);
}
}
return;
}
char c=s[k];
for(int j=head[u];j;j=edge[j].nxt){
int v=edge[j].to;
if(edge[j].se==c||edge[j].se=='?'){
check(v,k+,j);
}
if(edge[j].se=='*'){
for(int q=;q<=l-k;q++){
check(v,k+q,j);
}
}
}
return;
}
bool t[];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",s);
insert(i);
}
for(int j=;j<m;j++){
scanf("%s",s);
ans.clear();
l=strlen(s);
check(,,);
if(ans.empty()){
printf("Not match");
}else{
memset(t,,sizeof(t));
sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++){
if(!t[ans[i]]){
printf("%d ",ans[i]);
t[ans[i]]=;
}
}
}
printf("\n");
}
return ;
}
POJ1816:Wild Words——题解的更多相关文章
- poj1816 Wild Words
Wild Words Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5567 Accepted: 1475 Descri ...
- Clash Credenz 2014 Wild Card Round题解
A题 简单模拟. /************************************************************************* > File Name: ...
- Wild Words
poj1816:http://poj.org/problem?id=1816 题意:给你n个模板串,然后每个串除了字母,还有?或者*,?可以代替任何非空单个字符,*可以替代任何长度任何串,包括空字符串 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
随机推荐
- JS学习 用 arguments 对象模拟函数重载
用 arguments 对象判断传递给函数的参数个数,即可模拟函数重载: function doAdd() { if(arguments.length == 1) { alert(arguments[ ...
- jmeter 函数助手
1.选项,函数助手对话框,打开函数助手 2.使用方法 输入参数,点击生成,可以直接使用(Name of variable in which to store the result (optional) ...
- <cfloat> (float.h)
头文件: <cfloat> (float.h) 浮点类型的特性 这个头文件为特殊系统和编译器的实现描述了浮点类型的特征. 一个浮点数包含四个元素: 一个标志(a sign):正或负; 一个 ...
- python3 SQLAlchemy模块使用
更详细的操作介绍:https://www.imooc.com/article/22343 定义: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对 ...
- 【Linux 运维】Centos7初始化网络配置
设置网络 (1)动态获取一个IP地址 #dhclient 系统自动自动获取一个IP地址#ip addr 查看获取的ip地址(2)查看网关,子网掩码 虚拟机编辑>虚拟 ...
- Python最长连续数列的O(n)解法
题目 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入样例 100,4,200,1,3,2 54,55,300,12 1 5,4,3,2,1 1,2,3,4,5, ...
- 关于GitHub推送时发生Permission denied (publickey)的问题
今天在学习廖雪峰老师官网的git教程“添加远程库”时发现总是推送失败,下边提示“Permission denied (publickey) 这个问题” 传送门:https://www.liaoxuef ...
- Cortex-M3(NXP LPC 1788) 启动代码
startup_LPC177x_8x.s启动代码分析. 参考资料: Cortex-M3 (NXP LPC1788)之启动代码分析 ARM启动过程(Cortex-M3 NXP LPC1768为例) ;/ ...
- POJ 1113 Wall(计算几何の凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...
- USACO 1.1.3 Friday the Thirteenth 黑色星期五
Description 13号又是一个星期5.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至 ...