[HNOI2006]最短母串 (AC自动机+状压)
Description
给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串。
Input
Output
Sample Input
ABCD
BCDABC
Sample Output
拿这题练了练数组版的AC自动机
数据范围显然状压 插入时预处理出每个节点是哪个串的结束节点
然后建图 连fail指针的时候合并状态
为了最短显然需要按层转移,所以bfs
可以保证一达到末状态就return得到最优解
数组
- #include<queue>
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- #include<cstdlib>
- using namespace std;
- int n,tot=;
- char s[];
- struct trie
- {
- int son[],st,fail;
- }t[];
- void ins(char* str,int k)
- {
- int l=strlen(str+),root=;
- for(int i=;i<=l;i++)
- {
- int x=str[i]-'A';
- if(!t[root].son[x])t[root].son[x]=++tot;
- root=t[root].son[x];
- }
- t[root].st|=<<(k-);
- }
- void build()
- {
- queue<int> q;
- for(int i=;i<;i++)
- if(t[].son[i])q.push(t[].son[i]);
- while(!q.empty())
- {
- int x=q.front();
- q.pop();
- for(int i=;i<;i++)
- {
- int &y=t[x].son[i];
- if(!y)
- {
- y=t[t[x].fail].son[i];
- continue;
- }
- t[y].fail=t[t[x].fail].son[i];
- t[y].st|=t[t[y].fail].st;
- q.push(y);
- }
- }
- }
- bool v[][(<<)+];
- int let[(<<)+],fa[(<<)+],ans[],num=;
- void bfs()
- {
- queue<pair<int,int> > q;
- q.push(make_pair(,));
- int f=,ss=;
- while(!q.empty())
- {
- int x=q.front().first,nowst=q.front().second;
- q.pop();
- if(nowst==(<<n)-)
- {
- for(int i=f;i;i=fa[i])
- ans[++num]=let[i];
- for(int i=num;i;i--)putchar(ans[i]+'A');
- return ;
- }
- for(int i=;i<;i++)
- {
- int y=t[x].son[i],nxtst=nowst|t[y].st;
- if(!v[y][nxtst])
- {
- v[y][nxtst]=;
- ss++;
- fa[ss]=f;
- let[ss]=i;
- q.push(make_pair(y,nxtst));
- }
- }
- f++;
- }
- }
- int main()
- {
- scanf("%d",&n);
- for(int i=;i<=n;i++)
- {
- scanf("%s",s+);
- ins(s,i);
- }
- build();
- bfs();
- return ;
- }
指针
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- #include<queue>
- #include<cstdlib>
- using namespace std;
- const int N=;
- int n;
- char s[N];
- struct node
- {
- node *son[];
- int st;
- bool vis[(<<)+];
- node *fail;
- node()
- {
- memset(this,,sizeof(node));
- }
- };
- node *root;
- void ini()
- {
- root=new node();
- }
- void ins(char *str,int num)
- {
- int l=strlen(str+);
- node *now=root;
- for(int i=;i<=l;i++)
- {
- if(!now->son[str[i]-'A'])now->son[str[i]-'A']=new node();
- now=now->son[str[i]-'A'];
- }
- now->st|=<<(num-);
- }
- void build()
- {
- queue<node*>q;
- for(int i=;i<;i++)
- {
- if(root->son[i])
- {
- root->son[i]->fail=root;
- q.push(root->son[i]);
- }
- else root->son[i]=root;
- }
- while(!q.empty())
- {
- node *x=q.front();
- q.pop();
- for(int i=;i<;i++)
- {
- if(x->son[i])
- {
- x->son[i]->fail=x->fail->son[i];
- if(x->son[i]->fail)x->son[i]->st|=x->son[i]->fail->st;
- q.push(x->son[i]);
- }
- else x->son[i]=x->fail->son[i];
- }
- }
- }
- int fa[],ans[],tot=,qwq[];
- void bfs()
- {
- queue<node*> q;
- queue<int> ST;
- q.push(root);ST.push();
- root->vis[]=;
- int f=,ss=;
- while(!q.empty())
- {
- node *x=q.front();int nowSt=ST.front();
- q.pop();ST.pop();
- // cout<<(x->st)<<endl;
- if(nowSt==(<<n)-)
- {
- for(int i=f;i;i=fa[i])ans[++tot]=qwq[i];
- for(int i=tot;i;i--)putchar(ans[i]+'A');
- return ;
- }
- for(int i=;i<;i++)
- {
- if(!x->son[i])continue;
- int state=nowSt|(x->son[i]->st);
- if(!x->son[i]->vis[state])
- {
- x->son[i]->vis[state]=;
- ss++;
- fa[ss]=f;
- qwq[ss]=i;
- q.push(x->son[i]);
- ST.push(state);
- }
- }
- f++;
- }
- }
- int main()
- {
- scanf("%d",&n);
- ini();
- for(int i=;i<=n;i++)
- {
- scanf("%s",s+);
- ins(s,i);
- }
- build();
- bfs();
- return ;
- }
(话说这题数组大小神TM坑啊……)
[HNOI2006]最短母串 (AC自动机+状压)的更多相关文章
- bzoj1195 [HNOI2006]最短母串 AC 自动机+状压+bfs
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1195 题解 建立 AC 自动机,然后构建出 trie 图. 然后直接在 trie 图上走.但是 ...
- BZOJ 1195: [HNOI2006]最短母串 AC自动机+状压+搜索
思路比较直接. 由于 $n$ 很小,直接定义 $f[i][j]$ 表示当前在自动机中的节点 $i,$ 被覆盖串的集合为 $j$ 的方案数. #include <bits/stdc++.h> ...
- 【bzoj1195】[HNOI2006]最短母串 AC自动机+状态压缩+BFS最短路
原文地址:http://www.cnblogs.com/GXZlegend/p/6825226.html 题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串 ...
- BZOJ1195[HNOI2006]最短母串——AC自动机+BFS+状态压缩
题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字符串的 ...
- Bzoj1195 [HNOI2006]最短母串 [AC自动机]
Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1304 Solved: 439 Description 给定n个字符串(S1,S2,„,Sn),要求找 ...
- BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)
BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...
- BZOJ1195 [HNOI2006]最短母串 AC自动机 bfs
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - BZOJ1195 题意概括 给出一堆串,然后求一个包含这些串的所有串的最短的中的字典序最小的. 题解 先造一个AC ...
- BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图
BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2, ...
- 【状态压缩dp】1195: [HNOI2006]最短母串
一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...
随机推荐
- js location对象相关命令
Location.href 返回整个当前url,若对其赋值:location.href="http://www.sina.com.cn"则跳转其urllocation.host 返 ...
- swift 笔记 (二十一) —— 高级运算符
高级运算符 位运算符 按位取反: ~ 按位与运算: & 按位或运算: | 按位异或运算: ^ 按位左移运算: << 按位右移动算: >> 溢出运算符 自从swif ...
- 远程查看日志-linux
ssh 连接服务器 ssh user@www.xxx.com -p60022 用户名@ip 端口 进入日志所在目录 cat FILENAME 查看文本文件,P.S. 在查较大文件时为了避免刷屏,请使用 ...
- C#中,JSON字符串转换成对象。
在前台提交(post)的数据中.除了强类型的数据外,还有一个额外的json数据提交 在这里我的办法是,在前台把json对象转换成字符串,然后提交. 测试demo 前台: @using(Html.Beg ...
- YTU 2500: 二元表达式计算
2500: 二元表达式计算 时间限制: 1 Sec 内存限制: 128 MB 提交: 38 解决: 23 题目描述 根据输入的含有两个二元运算的表达式,编程计算并输出表达式的值.如输入: 2+9 ...
- memcached知识点梳理
Memcached概念: Memcached是一个免费开源的,高性能的,具有分布式对象的缓存系统,它可以用来保存一些经常存取的对象或数据,保存的数据像一张巨大的HASH表,该表以Key-valu ...
- 开源框架 KJFrameForAndroid
一个Android的快速开发工具包,使用它你可以轻松实现网络请求.插件化开发.图片加载等功能.KJFrameForAndroid的设计思想是通过封装Android原生SDK中复杂的复杂操作而达到简化A ...
- 不温不火WindowsPhone
最近在考虑是否转其他平台,如iOS或者Android或者javascript等. 已经以Windows Phone 开发作为工作就一年了(也不算是真正的Windows Phone开发吧,仅仅是开发高德 ...
- clc和clear命令的使用
clc命令是用来清除命令窗口的内容,这点不用多说.不管开启多少个应用程序,命令窗口只有一个,所以clc无论是在脚本m文件或者函数m文件调用时,clc命令都会清除命令窗口的内容.clear命令可以用来清 ...
- LVS的持久连接、会话保持和高可用介绍
持续连接 1)持久连接(lvs persistence)模板: 实现无论使用任何调度算法,在一段时间内(默认360s),能够实现将来自同一个地址的请求始终发往同一个RS ipvsadm -A|E -t ...