Description

给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串。

32MB

Input

第一行是一个正整数n(n<=12),表示给定的字符串的个数。
以下的n行,每行有一个全由大写字母组成的字符串。每个字符串的长度不超过50.

Output

只有一行,为找到的最短的字符串T。在保证最短的前提下,
如果有多个字符串都满足要求,那么必须输出按字典序排列的第一个。

Sample Input

2
ABCD
BCDABC

Sample Output

ABCDABC
 

Solution

一看是一个AC自动机。

一看是一个状压。

一看AC自动机节点再记录一个has包含的字符串集合。

一看要输出方案,肯定也要先考虑怎么弄出最短的长度。

f[i][(1<<n)-1]表示,匹配到AC自动机上的i点,包含的字符串集合为。。。的最短长度。

一看转移有环,然后无法再加入新的阶段,因为会MLE会TLE

所以要环形处理。

一看是一个取min的do

所以可以考虑最短路。

dij,spfa复杂度卡不过。

一看边权只有1……

BFS大法吼!

长度OK

怎么处理方案?

ywy_c_asm:

一遍bfs求出最短距离len,然后再一遍dfs找方案。

dfs时,相当于再把bfs的最短路怎么来的再访问一遍。如果dis[y]=dis[x]+1那么可以转移的,才可以访问。

还需要知道一个点到终点的最短路。

(反向多起点BFS???不行,或运算不可逆)

我们dfs时就可以实现的。类似树形dp

然后如果一个点到一个(1<<n)-1状态的点距离为juli的话,如果有dis[x]+juli[x]==len,那么,这次选择的这个y,所填的字符,就是最终答案的一个字符。

直接加入答案字符串。

char从A~Z枚举。保证第一次搜到的是字典序最小的。

而且一定是连续加入ans字符串。

dfs开头放上,如果tot==n return;

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N=;
  5. const int M=;
  6. const int U=**((<<)-)+;
  7. const int inf=0x3f3f3f3f;
  8. int n;
  9. char s[];
  10. struct trie{
  11. int fail[M],ch[M][];
  12. int has[M];
  13. int cnt;
  14. void ins(char *s,int l,int id){
  15. int now=;
  16. for(int i=;i<=l;i++){
  17. int x=s[i]-'A';
  18. if(!ch[now][x]) ch[now][x]=++cnt;
  19. now=ch[now][x];
  20. }
  21. has[now]|=(<<(id-));
  22. }
  23. void build(){
  24. queue<int>q;
  25. for(int i=;i<=;i++){
  26. if(ch[][i]) fail[ch[][i]]=,q.push(ch[][i]);
  27. }
  28. while(!q.empty()){
  29. int x=q.front();q.pop();
  30. has[x]|=has[fail[x]];
  31. for(int i=;i<=;i++){
  32. if(ch[x][i]){
  33. fail[ch[x][i]]=ch[fail[x]][i];
  34. q.push(ch[x][i]);
  35. }
  36. else ch[x][i]=ch[fail[x]][i];
  37. }
  38. }
  39. }
  40. }ac;
  41. int get(int ptr,int st){
  42. return ptr*(<<n)+st;
  43. }
  44. int dis[U];
  45. bool vis[U];
  46. struct node{
  47. int P,S;
  48. };
  49. queue<node>q;
  50. void bfs(){
  51.  
  52. memset(dis,0x3f,sizeof dis);
  53. int str=get(,);
  54. dis[str]=;
  55. vis[str]=;
  56. node nn;nn.P=,nn.S=;
  57. q.push(nn);
  58. while(!q.empty()){
  59. node lp=q.front();q.pop();
  60. for(int i=;i<=;i++){
  61. int to=ac.ch[lp.P][i];
  62. int NS=lp.S|ac.has[ac.ch[lp.P][i]];
  63. int NID=get(to,NS);
  64. if(!vis[NID]){
  65. dis[NID]=dis[get(lp.P,lp.S)]+;
  66. vis[NID]=;
  67. node kk;
  68. kk.P=to;kk.S=NS;
  69. q.push(kk);
  70. }
  71. }
  72. }
  73. }
  74. int len;
  75. int tot;
  76. char ans[M];
  77. int juli[U];
  78. void dfs(int ptr,int st){
  79.  
  80. int now=get(ptr,st);
  81. juli[now]=inf;
  82.  
  83. if(tot==len) return;
  84.  
  85. if(st==(<<n)-) {
  86. juli[now]=;return;
  87. }
  88. for(int i=;i<=;i++){
  89. int to=ac.ch[ptr][i];
  90. int NS=st|ac.has[to];
  91. int NID=get(to,NS);
  92. if(dis[NID]==dis[now]+){
  93. if(!vis[NID]){
  94. vis[NID]=;
  95. dfs(to,NS);
  96. }
  97. juli[now]=min(juli[now],juli[NID]+);
  98. if(dis[now]+juli[now]==len){
  99. ans[++tot]='A'+i;return;
  100. }
  101. }
  102. }
  103. }
  104. int main(){
  105. scanf("%d",&n);
  106. for(int i=;i<=n;i++){
  107. scanf("%s",s+);
  108. int l=strlen(s+);
  109. ac.ins(s,l,i);
  110. }
  111. ac.build();
  112. bfs();
  113. len=inf;
  114. //for(int j=0;j<=(1<<n)-1;j++)
  115. //for(int i=0;i<=ac.cnt;i++){
  116. // cout<<i<<" "<<j<<" : "<<dis[get(i,j)]<<endl;
  117. //}
  118. for(int i=;i<=ac.cnt;i++){
  119. int id=get(i,(<<n)-);
  120. len=min(len,dis[id]);
  121. }
  122. //cout<<" len "<<len<<endl;
  123. memset(vis,,sizeof vis);
  124. memset(juli,0x3f,sizeof juli);
  125. vis[get(,)]=;
  126. dfs(,);
  127. //int haha=dfs(0,0);
  128. for(int i=tot;i>=;i--){
  129. printf("%c",ans[i]);
  130. }
  131. return ;
  132. }

1

但是不够优美。

为什么要bfs然后再dfs呢?

bfs也可以求前驱啊!!
bfs时,第一更新到的就是最短路。

如果我们char A~Z,那么更新到的char

也就叫from[y],也就是到y这个点所形成的字典序最小字符串最后一个字符。

记录from,pre(也就是前驱)

bfs后,先找到len

再把所有f[i][(1<<n)-1]的字符找出来,cmp一下。

反正复杂度不超过600*600

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N=;
  5. const int M=;
  6. const int U=**((<<)-)+;
  7. const int inf=0x3f3f3f3f;
  8. int n;
  9. char s[];
  10. struct trie{
  11. int fail[M],ch[M][];
  12. int has[M];
  13. int cnt;
  14. void ins(char *s,int l,int id){
  15. int now=;
  16. for(int i=;i<=l;i++){
  17. int x=s[i]-'A';
  18. if(!ch[now][x]) ch[now][x]=++cnt;
  19. now=ch[now][x];
  20. }
  21. has[now]|=(<<(id-));
  22. }
  23. void build(){
  24. queue<int>q;
  25. for(int i=;i<=;i++){
  26. if(ch[][i]) fail[ch[][i]]=,q.push(ch[][i]);
  27. }
  28. while(!q.empty()){
  29. int x=q.front();q.pop();
  30. has[x]|=has[fail[x]];
  31. for(int i=;i<=;i++){
  32. if(ch[x][i]){
  33. fail[ch[x][i]]=ch[fail[x]][i];
  34. q.push(ch[x][i]);
  35. }
  36. else ch[x][i]=ch[fail[x]][i];
  37. }
  38. }
  39. }
  40. }ac;
  41. int get(int ptr,int st){
  42. return ptr*(<<n)+st;
  43. }
  44. int dis[U];
  45. bool vis[U];
  46. struct node{
  47. int P,S;
  48. };
  49. queue<node>q;
  50. int pre[U];
  51. int from[U];
  52. void bfs(){
  53. memset(dis,0x3f,sizeof dis);
  54. int str=get(,);
  55. dis[str]=;
  56. vis[str]=;
  57. pre[str]=-;//warning!!
  58. node nn;nn.P=,nn.S=;
  59. q.push(nn);
  60. while(!q.empty()){
  61. node lp=q.front();q.pop();
  62. for(int i=;i<=;i++){
  63. int to=ac.ch[lp.P][i];
  64. int NS=lp.S|ac.has[ac.ch[lp.P][i]];
  65. int NID=get(to,NS);
  66. if(!vis[NID]){
  67. dis[NID]=dis[get(lp.P,lp.S)]+;
  68. vis[NID]=;
  69. from[NID]=i+;//warning!!!!
  70. pre[NID]=get(lp.P,lp.S);
  71. node kk;
  72. kk.P=to;kk.S=NS;
  73. q.push(kk);
  74. }
  75. }
  76. }
  77. }
  78. int len;
  79. int tot;
  80. char ans[M];
  81. char a[M];
  82. bool fl;
  83. bool cmp(char *a,char *b){//a better than b?
  84. for(int i=;i<=len;i++){
  85. if(a[i]<b[i]) return ;
  86. if(a[i]>b[i]) return ;
  87. }
  88. }
  89. int main(){
  90. scanf("%d",&n);
  91. for(int i=;i<=n;i++){
  92. scanf("%s",s+);
  93. int l=strlen(s+);
  94. ac.ins(s,l,i);
  95. }
  96. ac.build();
  97. bfs();
  98. len=inf;
  99. for(int i=;i<=ac.cnt;i++){
  100. int id=get(i,(<<n)-);
  101. len=min(len,dis[id]);
  102. }
  103. fl=false;
  104. //cout<<" len "<<len<<endl;
  105. for(int i=;i<=ac.cnt;i++){
  106. int id=get(i,(<<n)-);
  107. if(dis[id]==len){
  108. int tmp=len;
  109. int z=id;
  110. while(pre[z]!=-){
  111. //cout<<z<<endl;
  112. a[tmp]='A'+(from[z]-);
  113. z=pre[z];tmp--;
  114. }
  115. if(!fl){
  116. fl=true;
  117. memcpy(ans,a,sizeof a);
  118. }
  119. else{
  120. if(cmp(a,ans)) memcpy(ans,a,sizeof a);
  121. }
  122. }
  123. }
  124. printf("%s",ans+);
  125. return ;
  126. }

2

但是还不够优美!!

为什么bfs之后还要再比较一遍字符串呢??

bfs中,第一次到达一个(1<<n)-1的点,

这个点就一定是最优解的最后一个节点!!!

因为,bfs分层图保证了最短。

for char A~Z保证了字典序最优。

直接输出即可。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N=;
  5. const int M=;
  6. const int U=**((<<)-)+;
  7. const int inf=0x3f3f3f3f;
  8. int n;
  9. char s[];
  10. struct trie{
  11. int fail[M],ch[M][];
  12. int has[M];
  13. int cnt;
  14. void ins(char *s,int l,int id){
  15. int now=;
  16. for(int i=;i<=l;i++){
  17. int x=s[i]-'A';
  18. if(!ch[now][x]) ch[now][x]=++cnt;
  19. now=ch[now][x];
  20. }
  21. has[now]|=(<<(id-));
  22. }
  23. void build(){
  24. queue<int>q;
  25. for(int i=;i<=;i++){
  26. if(ch[][i]) fail[ch[][i]]=,q.push(ch[][i]);
  27. }
  28. while(!q.empty()){
  29. int x=q.front();q.pop();
  30. has[x]|=has[fail[x]];
  31. for(int i=;i<=;i++){
  32. if(ch[x][i]){
  33. fail[ch[x][i]]=ch[fail[x]][i];
  34. q.push(ch[x][i]);
  35. }
  36. else ch[x][i]=ch[fail[x]][i];
  37. }
  38. }
  39. }
  40. }ac;
  41. int get(int ptr,int st){
  42. return ptr*(<<n)+st;
  43. }
  44. int dis[U];
  45. bool vis[U];
  46. struct node{
  47. int P,S;
  48. };
  49. queue<node>q;
  50. int pre[U];
  51. int from[U];
  52. int len;
  53. char ans[M];
  54. void bfs(){
  55. memset(dis,0x3f,sizeof dis);
  56. int str=get(,);
  57. dis[str]=;
  58. vis[str]=;
  59. pre[str]=-;//warning!!
  60. node nn;nn.P=,nn.S=;
  61. q.push(nn);
  62. while(!q.empty()){
  63. node lp=q.front();q.pop();
  64. for(int i=;i<=;i++){
  65. int to=ac.ch[lp.P][i];
  66. int NS=lp.S|ac.has[ac.ch[lp.P][i]];
  67. int NID=get(to,NS);
  68. if(!vis[NID]){
  69. dis[NID]=dis[get(lp.P,lp.S)]+;
  70. vis[NID]=;
  71. from[NID]=i+;//warning!!!!
  72. pre[NID]=get(lp.P,lp.S);
  73. node kk;
  74. kk.P=to;kk.S=NS;
  75. q.push(kk);
  76. if(NS==(<<n)-){
  77. int z=NID;
  78. while(pre[z]!=-){
  79. ans[++len]='A'+(from[z]-);
  80. z=pre[z];
  81. }
  82. return;
  83. }
  84. }
  85. }
  86. }
  87. }
  88.  
  89. int main(){
  90. scanf("%d",&n);
  91. for(int i=;i<=n;i++){
  92. scanf("%s",s+);
  93. int l=strlen(s+);
  94. ac.ins(s,l,i);
  95. }
  96. ac.build();
  97. bfs();
  98. for(int i=len;i>=;i--) printf("%c",ans[i]);
  99. return ;
  100. }

3

总结:

有的时候我们只关心最优答案。

但有的时候我们也关心方案。(毕竟知道方案比较实用)

方案的输出就要求高了一些。

但是肯定也是在最优答案的基础上的。

关于路径转移,凑字典序最小,经常通过松弛最优解的顺序,恰好可以保证松弛路径就是最小字典序。

本题就是一个很好的例子。

[HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理的更多相关文章

  1. [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索

    [HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...

  2. [bzoj1195][HNOI2006]最短母串_动态规划_状压dp

    最短母串 bzoj-1195 HNOI-2006 题目大意:给一个包含n个字符串的字符集,求一个字典序最小的字符串使得字符集中所有的串都是该串的子串. 注释:$1\le n\le 12$,$1\le ...

  3. [HNOI2006]最短母串问题 AC自动机

    题面:洛谷 题解: 如果我们对这些小串建出AC自动机,那么我们所求的大串就是要求满足遍历过所有AC自动机上的叶子节点,且经过步数最少的串.如果有多个步数相同的串,要输出字典序最小的串. 在AC自动机上 ...

  4. [BZOJ1195]:[HNOI2006]最短母串(AC自动机+BFS)

    题目传送门 题目描述 给定n个字符串(S1,S2,…,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,…,Sn)都是T的子串. 输入格式 第一行是一个正整数n,表示给定的字符串的个数 ...

  5. bzoj1195 [HNOI2006]最短母串 AC 自动机+状压+bfs

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1195 题解 建立 AC 自动机,然后构建出 trie 图. 然后直接在 trie 图上走.但是 ...

  6. P2322 [HNOI2006]最短母串问题

    P2322 [HNOI2006]最短母串问题 AC自动机+bfs 题目要求:在AC自动机建的Trie图上找到一条最短链,包含所有带结尾标记的点 因为n<12,所以我们可以用二进制保存状态:某个带 ...

  7. BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图

    BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2, ...

  8. 【状态压缩dp】1195: [HNOI2006]最短母串

    一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...

  9. bzoj 1195: [HNOI2006]最短母串 爆搜

    1195: [HNOI2006]最短母串 Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 894  Solved: 288[Submit][Status] ...

随机推荐

  1. Ansible开发之路

    一.初识Ansible 链接:https://www.cnblogs.com/baishuchao/articles/9164083.html 二.Ansible的架构 链接:https://www. ...

  2. vue中的样式

    一.使用class样式: CSS部分: <style> .green{ color:green; } .italic{ font-style:italic; } .thin{ ; } .a ...

  3. Pvmove中断后恢复LV状态

    Pvmove中断后恢复LV状态   pvmove执行时关闭中断窗口后,pvmove进程会被强制杀掉,从而导致lv的状态异常,无法重新进行pvmove和其他lvm镜像增加相关操作,可以通过如下方式修复: ...

  4. leetcode28_C++实现strStr()函数

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  5. Hands on Machine Learning with sklearn and TensorFlow —— 一个完整的机器学习项目(加州房地产)

    数据集地址:https://github.com/ageron/handson-ml/tree/master/datasets 先行知识准备:NumPy,Pandas,Matplotlib的模块使用 ...

  6. Android开发设计 实验报告

    20162315 Android开发设计 实验报告 实验内容 1.安装 Android Stuidio,完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学 ...

  7. C语言的问卷调查

    1.你对自己的未来有什么规划?做了哪些准备? 未来想当一个网络工程师,为了这个目标我正在努力学习网络.网页及相关的知识. 2.你认为什么是学习?学习有什么用?现在学习动力如何?为什么? 学习就是不断尝 ...

  8. lintcode-491-回文数

    491-回文数 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 注意事项 给的数一定保证是32位正整数,但是反转之后的数就未必了. 样例 11, 121, 1 ...

  9. java 两个数组合并

    需求:两个字符串合并(如果想去重复,参考下一篇--数组去重复及记录重复个数) //方法一 Arrays类 String[] a = {"A","B"," ...

  10. 10个linux网络和监控命令

    我下面列出来的10个基础的每个linux用户都应该知道的网络和监控命令.网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nsloo ...