BZOJ1195 [HNOI2006]最短母串 AC自动机 bfs
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
传送门 - BZOJ1195
题意概括
给出一堆串,然后求一个包含这些串的所有串的最短的中的字典序最小的。
题解
先造一个AC自动机,多模匹配嘛。
然后bfs在AC自动机上面走,两维状态,dis[i][j]表示已经走到过的串状态为i,在AC自动机上面的位置为j的最短距离。
然后这题居然要卡空间!
坑死了。
然后用了short
wa掉了。
发现short实在小的可怜,然后把几个大的数组的有必要的一个开了int,然后30MB卡着限制过去了~
开心!!
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
struct Trie{
int e,fail,next[];
void init(){
e=fail=;
memset(next,,sizeof next);
}
}tree[];
int cnt;
void AC_Prepare(){
cnt=;
tree[].init(),tree[].init();
for (int i=;i<;i++)
tree[].next[i]=;
}
void add(char ch[],int bh){
int len=strlen(ch),rt=,t;
for (int i=;i<len;i++){
t=ch[i]-'A';
if (!tree[rt].next[t]){
tree[++cnt].init();
tree[rt].next[t]=cnt;
}
rt=tree[rt].next[t];
}
tree[rt].e|=<<bh;
}
void build_AC(){
int q[],head=,tail=,rt,son,k;
tree[].fail=,q[++tail]=;
while (head<tail){
rt=q[++head];
for (int i=;i<;i++){
son=tree[rt].next[i];
if (!son){
tree[rt].next[i]=tree[tree[rt].fail].next[i];
continue;
}
k=tree[rt].fail;
while (!tree[k].next[i])
k=tree[k].fail;
tree[son].fail=tree[k].next[i];
tree[son].e|=tree[tree[k].next[i]].e;
q[++tail]=son;
}
}
}
int n,head,tail,pre[<<][];
short dis[<<][];
bool f[<<][];
char str[],chosen[<<][],ansstr[];
struct Queue{
short x,y;
void push(int a,int b){
x=a,y=b;
}
void pushout(int &a,int &b){
a=x,b=y;
}
}q[];
int main(){
scanf("%d",&n);
AC_Prepare();
for (int i=;i<n;i++){
scanf("%s",str);
add(str,i);
}
build_AC();
memset(f,,sizeof f);
memset(dis,,sizeof dis);
memset(q,,sizeof q);
head=tail=;
q[++tail].push(,);
dis[][]=,f[][]=,pre[][]=;
int ans=-,x,y,x_,y_;
while (head<tail){
q[++head].pushout(x,y);
for (int i=;i<;i++){
y_=tree[y].next[i];
x_=x|tree[y_].e;
if (f[x_][y_])
continue;
dis[x_][y_]=dis[x][y]+;
chosen[x_][y_]=i;
pre[x_][y_]=head;
f[x_][y_]=;
q[++tail].push(x_,y_);
if (x_==(<<n)-){
ans=tail;
break;
}
}
if (ans!=-)
break;
}
q[ans].pushout(x,y);
int ansdis=dis[x][y],xnow,ynow;
for (int j=ansdis,i=ans;j>=&&i;j--,i=pre[xnow][ynow]){
q[i].pushout(xnow,ynow);
ansstr[j]=chosen[xnow][ynow]+'A';
}
for (int i=;i<=ansdis;i++)
printf("%c",ansstr[i]);
return ;
}
BZOJ1195 [HNOI2006]最短母串 AC自动机 bfs的更多相关文章
- 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),要求找 ...
- bzoj1195 [HNOI2006]最短母串 AC 自动机+状压+bfs
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1195 题解 建立 AC 自动机,然后构建出 trie 图. 然后直接在 trie 图上走.但是 ...
- BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图
BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2, ...
- 【bzoj1195】[HNOI2006]最短母串 AC自动机+状态压缩+BFS最短路
原文地址:http://www.cnblogs.com/GXZlegend/p/6825226.html 题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串 ...
- [HNOI2006]最短母串 (AC自动机+状压)
Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. Input 第一行是一个正整数n(n<=12) ...
- BZOJ 1195: [HNOI2006]最短母串 AC自动机+状压+搜索
思路比较直接. 由于 $n$ 很小,直接定义 $f[i][j]$ 表示当前在自动机中的节点 $i,$ 被覆盖串的集合为 $j$ 的方案数. #include <bits/stdc++.h> ...
- [bzoj1195][HNOI2006]最短母串_动态规划_状压dp
最短母串 bzoj-1195 HNOI-2006 题目大意:给一个包含n个字符串的字符集,求一个字典序最小的字符串使得字符集中所有的串都是该串的子串. 注释:$1\le n\le 12$,$1\le ...
- Bzoj1195 [HNOI2006]最短母串 [状态压缩]
Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1304 Solved: 439 Description 给定n个字符串(S1,S2,„,Sn),要求找 ...
随机推荐
- Win Server 2008 R2 IIS 默认只能添加一个 443 HTTPS 端口
问题: 解决方案: 方法一: 然后在:C:\Windows\system32\inetsrv\config\applicationHost.config 找到 对应网站 <binding pro ...
- C# 时间戳与时间相互转化
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 时间戳d ...
- js计算数字长度
js调用toString方法转为字符串后取长度 var num = 123; alert(num.toString().length);
- Activity Window View WindowManager关系&Touch事件分发机制
http://www.cnblogs.com/linjzong/p/4191891.html https://www.cnblogs.com/kest/p/5141817.html https://b ...
- 2018-2019-2 网络对抗技术 20165230 Exp6 信息搜集与漏洞扫描
目录 1.实验内容 2.实验过程 任务一:各种搜索技巧的应用 通过搜索引擎进行信息搜集 搜索网址目录结构 使用IP路由侦查工具traceroute 搜索特定类型的文件 任务二:DNS IP注册信息的查 ...
- 改环境变量改出问题了,vi,ls这些命令都不能用了,怎么办
1.出现这个问题肯定是以前的基本环境变量都用不了了(大部分情况就是多了一个空格) 解决办法: cd /usr/bin 下执行vi命令 修改环境变量 然后source /etc/profile ...
- nginx入门二
反向代理: proxy_pass server { listen 80; location /n { proxy_pass http://127.0.0.1:8000/test; } location ...
- 搭建Modelsim SE仿真环境-使用do文件仿真
本章我们介绍仿真环境搭建是基于Modelsim SE的.Modelsim有很多版本,比如说Modelsim-Altera,但是笔者还是建议大家使用Modelsim-SE,Modelsim-Altera ...
- 【CTF WEB】ISCC 2016 web 2题记录
偶然看到的比赛,我等渣渣跟风做两题,剩下的题目工作太忙没有时间继续做. 第1题 sql注入: 题目知识 考察sql注入知识,题目地址:http://101.200.145.44/web1//ind ...
- k64 datasheet学习笔记4---Memory Map
1.前言 本文主要介绍K64地址空间的映射 2. System Memory Map 3. K64地址映射 4. Armv7m地址映射 4.1 Armv7M.System地址段(0XE0000000~ ...