题目链接:点击查看

题目大意:给出 nnn 个模式串和 mmm 个匹配串,题目要求输出一种模式串的排列方式,使得 mmm 个模式串从头开始匹配的话,可以匹配到相应的模式串

模式串的长度不超过 444,两两互不相同,含有通配符 “_”

题目分析:一开始想的是对模式串状压,模式串的每个位置都有 262626 种情况,一个模式串可以匹配 4264^{26}426 个匹配串,可以预处理哈希,但是后续的处理不太会快速建图,遂放弃

看了题解后发现可以反过来想,每个匹配串若想匹配模式串,那么每个位置要么匹配原字符,要么匹配通配符,也就是每个匹配串可以匹配至多 24=162^4=1624=16 个模式串,确实这样一来就比较好处理了

至于建图,可以建一个有 nnn 个点,16∗m16*m16∗m 条边的有向图,然后跑拓扑就可以了

因为题目保证了任意两个模式串互不相同,所以可以用字典树完成字符串的匹配,以及在字典树上dfs搜索

代码:

// Problem: E. Pattern Matching
// Contest: Codeforces - Educational Codeforces Round 103 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1476/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
bool flag;
int in[N],n,m,k;
char s[N];
vector<int>node[N];
int trie[N][27],id[N],tot;
int newnode() {tot++;memset(trie[tot],0,sizeof(trie[tot]));return tot;
}
void addedge(int x,int y) {node[x].push_back(y);in[y]++;
}
void insert(int i) {int pos=0;for(int i=1;i<=k;i++) {int to=s[i]=='_'?26:s[i]-'a';if(!trie[pos][to]) {trie[pos][to]=newnode();}pos=trie[pos][to];}id[pos]=i;
}
void search(int pos,int step,int x) {if(step>k) {if(id[pos]==x) {flag=true;} else {addedge(x,id[pos]);}return;}if(trie[pos][s[step]-'a']) {search(trie[pos][s[step]-'a'],step+1,x);}if(trie[pos][26]) {search(trie[pos][26],step+1,x);}
}
void topo() {vector<int>ans;queue<int>q;for(int i=1;i<=n;i++) {if(!in[i]) {q.push(i);}}while(q.size()) {int u=q.front();q.pop();ans.push_back(u);for(auto v:node[u]) {if(--in[v]==0) {q.push(v);}}}if(ans.size()==n) {puts("YES");for(auto it:ans) {printf("%d ",it);}} else {puts("NO");}
}
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);read(n),read(m),read(k);for(int i=1;i<=n;i++) {scanf("%s",s+1);insert(i);}while(m--) {scanf("%s",s+1);int id;read(id);flag=false;search(0,1,id);if(!flag) {return 0*puts("NO");}}topo();return 0;
}

CodeForces - 1476E Pattern Matching(字典树+拓扑)相关推荐

  1. CodeForces - 888G Xor-MST(贪心+字典树+最小生成树)

    题目链接:点击查看 题目大意:给出 nnn 个点,任意两个点之间的边权为 ai⊕aja_i\oplus a_jai​⊕aj​,求最小生成树 题目分析:去年多校写过一样的模型,再拿出来写一遍回顾一下:牛 ...

  2. CodeForces - 1417E XOR Inverse(字典树求逆序对+分治)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a,现在要求选出一个 x,将 a 中的每个元素都异或之后得到一个新的数列 b,要求数列 b 的逆序对最小,问最小的逆序对是多少,x 该如何选择 ...

  3. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  4. E. Pattern Matching(题意理解+拓扑排序)

    E. Pattern Matching 首先p[mtj]p[mt_j]p[mtj​]必须能够匹配所给字符sjs_jsj​,然后把所有能够匹配的sjs_jsj​的其他模板串也找出来,这些必须放在p[mt ...

  5. 【CodeForces】445B A Lot of Games 字典树博弈

    传送门:[CodeForces]445B  A Lot of Games 题目大意:两人一起构造一个串,每人每次向串的末尾放一个字母,必须保证放了这个字母后能够成所给的N个串的前缀,如果某个人不能放时 ...

  6. Codeforces 455B A Lot of Games(字典树+博弈)

    题目连接: Codeforces 455B A Lot of Games 题目大意:给定n,表示字符串集合.给定k,表示进行了k次游戏,然后是n个字符串.每局开始,字符串为空串,然后两人轮流在末尾追加 ...

  7. Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)

    Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...

  8. Codeforces 861D - Polycarp's phone book 字典树/hash

    输入7e4个字符串,要求每个串提取一个子串来唯一表示 4s题可以hash暴力水过,大体思路就是把所有子串map自己的母串,过程中如果这个子串已有hash值就标-1 然后枚举map元素,维护最小化一下就 ...

  9. CodeForces - 456D A Lot of Games(字典树+博弈)

    题目链接:点击查看 题目大意:给出n个字符串,现在有两个人玩一个游戏,游戏规则是两人轮流构造同一个字符串,每次可以向末尾添加一个字母,必须保证添加字母后的字符串是n个字符串其中之一的前缀,不能操作者算 ...

最新文章

  1. 2021年诺贝尔经济学奖评述:解决重大社会问题的自然实验因果框架
  2. spoj16935 Straight Line Spiral Pattern (Act 3)
  3. 阿里云+wordpress搭建个人博客网站【小白专用的图文教程】
  4. 字符串的模式匹配(Java实现)
  5. iview在vue-cli3如何按需加载
  6. 多级嵌套json格式
  7. 关于SET和UNORDER_SET
  8. 利用制表位快速居中对齐公式,同时公式编号靠右对齐
  9. Mybatis if标签和where标签结合巧妙使用
  10. word手写字体以假乱真_轻松制作个人“电子版手写签名”
  11. 2020年施工晴雨表电子版_2020年建筑施工特种作业人员培训通知
  12. python核心编程第二版第六章答案
  13. 手势密码解锁微信小程序项目源码
  14. 中兴新支点操作系统_【中兴新支点操作系统】中兴新支点操作系统下载 v3.3.1 官方版-趣致软件园...
  15. 华为机试od社招刷题攻略-目录
  16. Linux开放MySql 3306端口
  17. CS224W-图神经网络 笔记5.1:Spectral Clustering - 谱聚类基础知识点
  18. harbor离线包下载(百度网盘)
  19. python helper方法_Python io_utils.ImportHelper方法代碼示例
  20. 环境信息术语(HJ/T 416—2007)

热门文章

  1. php 斗鱼人数,斗鱼旭旭宝宝再度登顶指数榜首位 单日弹幕人数高达48万人
  2. html 让表格在右侧显示不出来,css中怎么解决表格边框不显示的问题?
  3. Nacos-认识和安装Nacos
  4. 核心组件:IRule
  5. MyBatis 缓存详解-开启二级缓存的方法
  6. 中文和英文对应的字节
  7. SpringMVC的数据响应
  8. spring的aop注解配置(了解)
  9. ios上编译c语言的app,iOS App编译流程
  10. Spring WebApplicationContext