[BZOJ1195]最短母串
1195: [HNOI2006]最短母串
Time Limit: 10 Sec Memory Limit: 32 MB
Description
给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串。
Input
第一行是一个正整数n(n<=12),表示给定的字符串的个数。以下的n行,每行有一个全由大写字母组成的字符串。每个字符串的长度不超过50.
Output
只有一行,为找到的最短的字符串T。在保证最短的前提下,如果有多个字符串都满足要求,那么必须输出按字典序排列的第一个。
Sample Input
ABCD
BCDABC
Sample Output
#include<cstdio>
#include<cstring>
using namespace std;
const int K=<<;
const int N=;
short n,cnt,bit[];
char s[][],ans[N],c[N*K];
short val[N],ch[N][],f[N];
bool vis[N][K];
int from[N*K],q[N],hd,tl;
short state[N*K],point[N*K];
inline void get_fail()
{
hd=,tl=;
for(int i=;i<;i++)
if(ch[][i])q[tl++]=ch[][i];
while(hd^tl)
{
int rt=q[hd++];
for(int i=;i<;i++)
{
int u=f[rt];
if(ch[rt][i])
{
q[tl++]=ch[rt][i];
while(u&&!ch[u][i])u=f[u];
f[ch[rt][i]]=ch[u][i];
val[ch[rt][i]]|=val[ch[u][i]];
}
else
ch[rt][i]=ch[u][i];
}
}
}
inline void get_ans()
{
hd=,tl=;
while(hd^tl)
{
int u=point[hd],st=state[hd];
if(st==bit[n]-)
{
int id=;
while(hd)ans[++id]=c[hd],hd=from[hd];
while(id>=)putchar(ans[id]),id--;
return;
}
for(int i=;i<;i++)
{
if(!vis[ch[u][i]][st|val[ch[u][i]]])
{
point[tl]=ch[u][i];
c[tl]=i+'A',from[tl]=hd;
state[tl]=st|val[ch[u][i]];
vis[point[tl]][state[tl]]=;
tl++;
}
}
hd++;
}
}
int main()
{
scanf("%d",&n);
bit[]=;for(int i=;i<=n;i++)bit[i]=bit[i-]<<;
for(int i=;i<=n;i++)
{
scanf("%s",s[i]);
int rt=;int m=strlen(s[i]);
for(int j=;j<m;j++)
{
int d=s[i][j]-'A';
if(!ch[rt][d])ch[rt][d]=++cnt;
rt=ch[rt][d];
}
val[rt]|=bit[i-];
}
get_fail();get_ans();
}
[BZOJ1195]
[BZOJ1195]最短母串的更多相关文章
- [bzoj1195][HNOI2006]最短母串_动态规划_状压dp
最短母串 bzoj-1195 HNOI-2006 题目大意:给一个包含n个字符串的字符集,求一个字典序最小的字符串使得字符集中所有的串都是该串的子串. 注释:$1\le n\le 12$,$1\le ...
- BZOJ1195[HNOI2006]最短母串——AC自动机+BFS+状态压缩
题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字符串的 ...
- 【状态压缩dp】1195: [HNOI2006]最短母串
一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...
- bzoj 1195: [HNOI2006]最短母串 爆搜
1195: [HNOI2006]最短母串 Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 894 Solved: 288[Submit][Status] ...
- 2782: [HNOI2006]最短母串
2782: [HNOI2006]最短母串 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3 Solved: 2[Submit][Status][Web ...
- BZOJ 1195: [HNOI2006]最短母串
1195: [HNOI2006]最短母串 Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1346 Solved: 450[Submit][Status ...
- P2322 [HNOI2006]最短母串问题
P2322 [HNOI2006]最短母串问题 AC自动机+bfs 题目要求:在AC自动机建的Trie图上找到一条最短链,包含所有带结尾标记的点 因为n<12,所以我们可以用二进制保存状态:某个带 ...
- [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索
[HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...
- 【loj10061】最短母串
#10061. 「一本通 2.4 练习 4」最短母串 内存限制:512 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 1bentong 提交 提交 ...
随机推荐
- VirtualBox主机和虚拟机互相通信
默认情况下VirtualBox虚拟机网络设置为网络地址转换,虚拟机中的地址一般是10.0.2.x,虚拟机中访问主机只需要访问默认网关地址即可,但是主机访问虚拟机就需要增加一些配置了,方法有以下几种: ...
- python5
print应用 // 输出两行 print "hahaha" print "hehehe" // 输出在同一行里 print "hahaha" ...
- MAC下Android的Eclipse开发环境搭建
原文链接:https://www.cnblogs.com/macro-cheng/archive/2011/09/30/android-001.html 一.Eclipse的下载 到网站:http:/ ...
- 解决登录linux输入密码问题
1.使用密钥 ssh-keyssh -i .ssh/*.key root@<ip_addr> 2.使用sshpass 安装 rpm 包:yum install sshpass 配置文件: ...
- PHP学习和使用总结
起因 学习和使用PHP也有不少年头了,而自己也在学习和使用其他许多语言.我想通过这个总结来给自己一个交代.另一方面也分享一下开发经验,如何用PHP开发和管理大型的项目. 闲聊 许多人说自己1天学会PH ...
- Paper Reading - Deep Captioning with Multimodal Recurrent Neural Networks ( m-RNN ) ( ICLR 2015 ) ★
Link of the Paper: https://arxiv.org/pdf/1412.6632.pdf Main Points: The authors propose a multimodal ...
- Amazon.com Seller Distributed Inventory Placement Inventory Placement Service
Greetings, Thank you for writing to us. I understand that you would like to send inventory to our wa ...
- 第五次ScrumMeeting博客
第五次ScrumMeeting博客 本次会议于10月29日(日)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕. 1. 每个人的工作(有Issue的内容和 ...
- LeetCode 454. 4Sum II (C++)
题目: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are su ...
- HDU 5661 Claris and XOR 贪心
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5661 bc(中文):http://bestcoder.hdu.edu.cn/contests ...