题目链接

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has n 01 strings si, and he wants to know the number of 01 antisymmetric strings of length 2L which contain all given strings si as continuous substrings.

A 01 string s is antisymmetric if and only if s[i]≠s[|s|−i+1] for all i∈[1,|s|].

It is too difficult for Rikka. Can you help her?

In the second sample, the strings which satisfy all the restrictions are 000111,001011,011001,100110.

 
Input
The first line contains a number t(1≤t≤5), the number of the testcases.

For each testcase, the first line contains two numbers n,L(1≤n≤6,1≤L≤100).

Then n lines follow, each line contains a 01 string si(1≤|si|≤20).

 
Output
For each testcase, print a single line with a single number -- the answer modulo 998244353.
 
Sample Input
2
2 2
011
001
2 3
011
001
 
Sample Output
1
4
 
 
题意:反对称:对于一个长为2*N的串s[0~2*N-1],s[i]^s[2*N-1-i]=1 。现在有n个01串,求有多少个长为2*L的并且包含这n个串的 反对称01串?
 
思路:对于一个串包含在2*L的01串中,那么这个串要么在2*L的前半部分,要么后半部分,或者跨越中间,如果在后半部分,则需要找到其在前半部分的反对称01串,对于跨越中间的01串,则需要找到其在前面部分的串,例如:0 | 11,以竖线作为串中间,那么如果前面部分如果以00结束,那么一定含有 011这个串。把每个串的所有形式放入AC自动机对应的tire树中,然后状压递推。
 
代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <queue>
  6. #include <string>
  7. using namespace std;
  8. const int mod=;
  9. const int N=;
  10. struct Node{
  11. int id;
  12. Node *fail;
  13. Node *son[];
  14. int tag1,tag2;
  15. }node[N];
  16. queue<Node *>q;
  17. int tot;
  18. int dp[][][];
  19.  
  20. void insert1(string s,int id)
  21. {
  22. int len=s.length();
  23. Node *now=&node[];
  24. for(int i=;i<len;i++)
  25. {
  26. int x=s[i]-'';
  27. if(now->son[x]==NULL) now->son[x]=&node[tot++];
  28. now=now->son[x];
  29. }
  30. now->tag1|=(<<id);
  31. }
  32. void insert2(string s,int id)
  33. {
  34. int len=s.length();
  35. Node *now=&node[];
  36. for(int i=;i<len;i++)
  37. {
  38. int x=s[i]-'';
  39. if(now->son[x]==NULL) now->son[x]=&node[tot++];
  40. now=now->son[x];
  41. }
  42. now->tag2|=(<<id);
  43. }
  44. void init()
  45. {
  46. for(int i=;i<N;i++)
  47. {
  48. node[i].id=i;
  49. node[i].fail=NULL;
  50. node[i].son[]=node[i].son[]=NULL;
  51. node[i].tag1=node[i].tag2=;
  52. }
  53. }
  54. void setFail()
  55. {
  56. Node* root=&node[];
  57. q.push(root);
  58. while(!q.empty())
  59. {
  60. Node* now=q.front(); q.pop();
  61. for(int i=;i<;i++)
  62. {
  63. if(now->son[i])
  64. {
  65. Node* p=now->fail;
  66. while(p && (!(p->son[i]))) p=p->fail;
  67. now->son[i]->fail=(p)?(p->son[i]):(root);
  68. now->son[i]->tag1|=now->son[i]->fail->tag1;
  69. now->son[i]->tag2|=now->son[i]->fail->tag2;
  70. q.push(now->son[i]);
  71. }
  72. else now->son[i]=(now!=root)?now->fail->son[i]:(root);
  73. }
  74. }
  75. }
  76. void print()
  77. {
  78. Node* now=&node[];
  79. queue<Node*>qq;
  80. qq.push(now);
  81. while(!qq.empty())
  82. {
  83. now=qq.front(); qq.pop();
  84. cout<<"Y:"<<now->id<<" ";
  85. for(int i=;i<;i++)
  86. {
  87. if(now->son[i]) qq.push(now->son[i]),cout<<now->son[i]->id<<" ";
  88. else cout<<"NULL"<<" ";
  89. }
  90. cout<<endl;
  91. }
  92. }
  93. int main()
  94. {
  95. ///cout << "Hello world!" << endl;
  96. int t; cin>>t;
  97. while(t--)
  98. {
  99. init();
  100. tot=;
  101. int n,L; scanf("%d%d",&n,&L);
  102. for(int i=;i<n;i++)
  103. {
  104. string s; cin>>s;
  105. insert1(s,i);
  106. string t=s;
  107. reverse(t.begin(),t.end());
  108. int len=s.length();
  109. for(int j=;j<len;j++)
  110. t[j]=(char)((t[j]-'')^+'');
  111. insert1(t,i);
  112.  
  113. int mnLen=min(len,L);
  114. for(int j=;j<mnLen;j++)
  115. {
  116. int f=;
  117. for(int l=j,r=j+; l>=&&r<len; l--,r++)
  118. {
  119. if((s[l]^s[r])==) { f=; break; }
  120. }
  121. if(!f) continue;
  122. t=s.substr(,j+);
  123. for(int k=*j+;k<len;k++)
  124. {
  125. t=(char)((s[k]-'')^+'')+t;
  126. }
  127. insert2(t,i);
  128. }
  129. }
  130. ///print();
  131. setFail();
  132. memset(dp,,sizeof(dp));
  133. dp[][][]=;
  134. int cn=,stu=(<<n);
  135. for(int i=;i<L;i++)
  136. {
  137. int c=cn^;
  138. memset(dp[c],,sizeof(dp[c]));
  139. for(int j=;j<tot;j++)
  140. {
  141. for(int s=;s<stu;s++)
  142. {
  143. if(!dp[cn][j][s]) continue;
  144. if(i<L-)
  145. for(int k=;k<;k++)
  146. {
  147. int x=node[j].son[k]->id;
  148. int tag=node[x].tag1;
  149. dp[c][x][s|tag]=(dp[c][x][s|tag]+dp[cn][j][s])%mod;
  150. }
  151. else
  152. for(int k=;k<;k++)
  153. {
  154. int x=node[j].son[k]->id;
  155. int tag=node[x].tag1|node[x].tag2;
  156. dp[c][x][s|tag]=(dp[c][x][s|tag]+dp[cn][j][s])%mod;
  157. }
  158. }
  159. }
  160. cn=c;
  161. }
  162. int ans=;
  163. for(int i=;i<tot;i++)
  164. {
  165. ans=(ans+dp[cn][i][stu-])%mod;
  166. }
  167. printf("%d\n",ans);
  168. }
  169. return ;
  170. }

hdu 6086 -- Rikka with String(AC自动机 + 状压DP)的更多相关文章

  1. HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解

    题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...

  2. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...

  3. HDU 2825 Wireless Password(AC自动机 + 状压DP)题解

    题意:m个密码串,问你长度为n的至少含有k个不同密码串的密码有几个 思路:状压一下,在build的时候处理fail的时候要用 | 把所有的后缀都加上. 代码: #include<cmath> ...

  4. HDU - 2825 Wireless Password (AC自动机+状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2825 题意:给一些字符串,构造出长度为n的字符串,它至少包含k个所给字符串,求能构造出的个数. 题解: ...

  5. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdu 4057--Rescue the Rabbit(AC自动机+状压DP)

    题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...

  7. BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】

    题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...

  8. zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)

    Time Limit: 10 Seconds      Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...

  9. hdu2825 Wireless Password(AC自动机+状压dp)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

随机推荐

  1. java集合框架(1) hashMap 简单使用以及深度分析(转)

    java.util 类 HashMap<K,V>java.lang.Object  java.util.AbstractMap<K,V>      java.util.Hash ...

  2. 【python原理解析】gc原理初步解析

    python的gc是会用到:引用计数.标记-清除和分代收集,首先说明一下什么是引用计数 可以通过sys模块中的getrefcount()方法获取某个对象的引用计数 python本身的数据类型有基础类型 ...

  3. 716. Max Stack实现一个最大stack

    [抄题]: Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x ...

  4. Linux驱动之异步通知的应用

    前面的按键驱动方式都是应用程序通过主动查询的方式获得按键值的: 1.查询方式 2.中断方式 3.poll机制 下面介绍第四种按键驱动的方式 4.异步通知:它可以做到应用程序不用随时去查询按键的状态,而 ...

  5. MySQL数据查询之多表查询

    多表查询 多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment PRIMARY KEY, dna ...

  6. java读取jar包中的文件

    随手写了一个java小工具,maven打包成功后,发现工具总是读不到打在jar包中的文件信息,要读取的文件位于 /src/main/resources 目录下,打包成功后,文件就在jar包中根目录下, ...

  7. Python3实战系列之五(获取印度售后数据项目)

    问题:续接上一篇.说干咱就干呀,勤勤恳恳写程序呀! 目标:此篇我们试着把python程序打包成.exe程序.这样就可以在服务器上运行了.实现首篇计划列表功能模块的第二步: 2.将python程序转为 ...

  8. windows 性能监视器

    转载地址:https://www.cnblogs.com/luo-mao/p/5872374.html

  9. Java8特性之Lambda、方法引用和Streams

    这里涉及三个重要特性: Lambda 方法引用 Streams ① Lambda 最早了解Lambda是在C#中,而从Java8开始,Lambda也成为了新的特性,而这个新的特性的目的,就是为了消除单 ...

  10. php.ini 配置详解

    这个文件必须命名为''php.ini''并放置在httpd.conf中的PHPIniDir指令指定的目录中.最新版本的php.ini可以在下面两个位置查看:http://cvs.php.net/vie ...