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),要求找 ...
随机推荐
- linux系统--用户和用户组
一.用户和用户组的概念 用户:使用操作系统的人 用户组:具有相同系统权限的一组用户.在linux系统中可以存在多个用户组 1.1 /etc/group 这里存储当前系统中所有用户组的信息 每一行对应一 ...
- Java基础编程题——分别统计出其中汉字、英文字母、空格、数字和其它字符的个数
package com.yangzl.basic; import java.util.Scanner; /** * 分别统计出其中汉字.英文字母.空格.数字和其它字符的个数 * @author Adm ...
- python的__mro__与__slot__
class A(object): def __init__(self): print ' -> Enter A' print ' <- Leave A' class B(A): def _ ...
- POST 上传 JSON 数据
// // ViewController.m // 03-post上传json // // Created by jerry on 15/10/10. // Copyright (c) 2015年 j ...
- Window和document的区别
1.window 窗口对象.就是可视化区域的大小,不包含滚动条内东东. 2.document 对象,包含滚动条以外的区域
- OpenCV:Debug和Release模式 && 静态和动态编译
1.Release和Debug的区别 Release版称为发行版,Debug版称为调试版. Debug中可以单步执行.跟踪等功能,但生成的可执行文件比较大,代码运行速度较慢.Release版运行速度较 ...
- k64 datasheet学习笔记4---Memory Map
1.前言 本文主要介绍K64地址空间的映射 2. System Memory Map 3. K64地址映射 4. Armv7m地址映射 4.1 Armv7M.System地址段(0XE0000000~ ...
- freeRTOS中文实用教程1--任务
1.前言 FreeRTOS是小型多任务嵌入式操作系统,硬实时性.本章主要讲述任务相关特性及调度相关的知识. 2. 任务的总体特点 任务的状态 (1)任务有两个状态,运行态和非运行态 (2)任务由非运行 ...
- 使用NGINX+Openresty和unixhot_waf开源防火墙实现WAF功能
使用NGINX+Openresty实现WAF功能 一.了解WAF1.1 什么是WAF Web应用防护系统(也称:网站应用级入侵防御系统 .英文:Web Application Firewall,简称: ...
- 使用ueditor的时候,style样式传递到后台时被过滤没了
在项目中,使用ueditor的时候,style样式传递到后台时被过滤没了 转:https://www.cnblogs.com/theroad/p/5761743.html 经过chrome的一番调试后 ...