HNOI 2006 BZOJ 1195 最短母串
题面
问题描述
给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串。
输入
第一行是一个正整数n(n<=12),表示给定的字符串的个数。以下的n行,每行有一个全由大写字母组成的字符串。每个字符串的长度不超过50.
输出
只有一行,为找到的最短的字符串T。在保证最短的前提下,如果有多个字符串都满足要求,那么必须输出按字典序排列的第一个。
Sample Input
2
ABCD
BCDABC
Sample Output
ABCDABC
题解
AC自动机第一题...
实际上这题用到的是trie图, 状压DP即可. 每个节点记录在什么状态下被经过, 用于去重. 时间复杂度和空间复杂度都是\(2^nnL\)
#include <cstdio>
#include <cstring>
#include <deque>
const int N = 12, LEN = 50;
int n;
char ans[N * LEN];
struct ACautomaton
{
struct node
{
node *suc[26], *fl;
int vst[1 << N], ed;
inline node()
{
for(int i = 0; i < 26; ++ i)
suc[i] = NULL;
memset(vst, 0, sizeof(vst));
ed = -1;
}
}*rt;
inline ACautomaton()
{
rt = new node;
rt->fl = rt;
}
inline void insert(char *str, int len, int id)
{
node *u = rt;
for(int i = 0; i < len; u = u->suc[str[i] - 'A'], ++ i)
if(u->suc[str[i] - 'A'] == NULL)
u->suc[str[i] - 'A'] = new node;
u->ed = id;
}
inline void build()
{
static std::deque<node*> que;
que.clear();
for(int i = 0; i < 26; ++ i)
if(rt->suc[i] != NULL)
rt->suc[i]->fl = rt, que.push_back(rt->suc[i]);
for(; ! que.empty(); que.pop_front())
{
node *u = que.front();
for(int i = 0; i < 26; ++ i)
if(u->suc[i] != NULL)
{
node *p = u->fl;
for(; p != rt && p->suc[i] == NULL; p = p->fl);
u->suc[i]->fl = p->suc[i] == NULL ? p : p->suc[i];
que.push_back(u->suc[i]);
}
for(int i = 0; i < 26; ++ i)
if(u->suc[i] == NULL)
u->suc[i] = u->fl->suc[i];
}
}
struct state
{
node *u;
int lst, rec, c;
inline state(node *_u, int _lst, int _rec, int _c)
{
u = _u, lst = _lst, rec = _rec, c = _c;
}
inline state() {}
};
inline void work()
{
static state que[(1 << N) * N * LEN];
int L = 0, R = 0;
que[R ++] = state(rt, -1, 0, -1);
for(; ; L ++)
{
state cur = que[L];
node *u = cur.u;
int rec = cur.rec;
if(~ u->ed)
rec |= 1 << u->ed, u->vst[rec] = 1;
if(rec == (1 << n) - 1)
break;
for(int i = 0; i < 26; ++ i)
if(u->suc[i] != NULL && ! u->suc[i]->vst[rec])
que[R ++] = state(u->suc[i], L, rec, i), u->suc[i]->vst[rec] = 1;
}
int len = 0;
for(; L; L = que[L].lst)
ans[len ++] = 'A' + que[L].c;
for(int i = len - 1; ~ i; -- i)
putchar(ans[i]);
}
}ACA;
int main()
{
#ifndef ONLINE_JUDGE
freopen("BZOJ1195.in", "r", stdin);
freopen("BZOJ1195.out", "w", stdout);
#endif
scanf("%d\n", &n);
for(int i = 0; i < n; ++ i)
{
static char str[LEN];
scanf("%s", str);
ACA.insert(str, strlen(str), i);
}
ACA.build();
ACA.work();
}
HNOI 2006 BZOJ 1195 最短母串的更多相关文章
- [BZOJ 1195] 最短母串
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1195 Solution: 看到数据范围n<=12,就要往状压DP上想 为了保证后项 ...
- bzoj 1195: [HNOI2006]最短母串 爆搜
1195: [HNOI2006]最短母串 Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 894 Solved: 288[Submit][Status] ...
- BZOJ 1195: [HNOI2006]最短母串
1195: [HNOI2006]最短母串 Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1346 Solved: 450[Submit][Status ...
- bzoj 1195 [HNOI2006]最短母串 bfs 状压 最短路 AC自动机
LINK:最短母串 求母串的问题.不适合SAM. 可以先简化问题 考虑给出的n个字符串不存在包含关系. 那么 那么存在的情况 只可能有 两个字符串拼接起来能表示另外一个字符串 或者某个字符串的后缀可以 ...
- 【状态压缩dp】1195: [HNOI2006]最短母串
一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...
- 【loj10061】最短母串
#10061. 「一本通 2.4 练习 4」最短母串 内存限制:512 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 1bentong 提交 提交 ...
- [BZOJ1195]最短母串
1195: [HNOI2006]最短母串 Time Limit: 10 Sec Memory Limit: 32 MB Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最 ...
- 2782: [HNOI2006]最短母串
2782: [HNOI2006]最短母串 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3 Solved: 2[Submit][Status][Web ...
- BZOJ1195[HNOI2006]最短母串——AC自动机+BFS+状态压缩
题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 输入 第一行是一个正整数n(n<=12),表示给定的字符串的 ...
随机推荐
- Azure继续降价云 价格战就此终结?
[TechTarget中国原创] 刚刚跨入2016年,就听到了云降价这样一个消息,但是我们却不要期望降价之风如去年一样呼呼不绝. 微软公司在本周宣称,他们将在下个月对其D系列虚拟机实施高达17%的降价 ...
- ECMAScript5.1
http://lzw.me/pages/ecmascript/ ECMAScript5.1中文版 https://msdn.microsoft.com/zh-cn/library/dn656907. ...
- leetcode 【 Merge k Sorted Lists 】python 实现
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- oracle常用关键字和函数
数据库的增删改查: 增:insert into ... values(); 例:insert into p_emp values(sq_emp.nextval,,sysdate,,null,,); c ...
- sql注入过滤了#,--+怎么办
题目是NCTF2018的web题目 第一段是错误的思路,第二段是晚上有思考后发现的直接看第二段吧. ① ?id=1'会直接出来报错提示. 猜测使用单引号保护id. 另外一打空格就提示you hacke ...
- thinkphp3.2.3多图上传并且生成多张缩略图
html部分 <!DOCTYPE html><html><head><meta http-equiv="Content-Type" con ...
- webform登陆界面样式丢失
本文摘抄自:http://blog.csdn.net/sssix/article/details/16945347 请阅读原文. Forms验证——登录界面样式实效? <authenticati ...
- POJ--2823--Sliding Window----单调队列问题
Sliding Window Time Limit:12000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Des ...
- BZOJ 3990: [SDOI2015]排序(搜索+剪枝)
[SDOI2015]排序 Description 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1< ...
- xctf --Hctf2014 Quals write up
描述 猫流大大发现一个女神,你能告诉我女神的名字么(名字即是flag) nvshen.zip Solution: Extract the file and we could find a txt wh ...